在代码后面绑定静态属性

Moh*_*med 0 c# wpf xaml binding

我试图按照这篇文章,唯一的区别是我在后面的代码中创建和绑定控件.不幸的是它不起作用.这是我的示例代码:

 public partial class ShellWindow
 {
      private static Visibility progressbarVisibility = Visibility.Collapsed;
      public static Visibility ProgressbarVisibility
      {
           get { return progressbarVisibility; }
           set
           {
               if (progressbarVisibility == value) return;
               progressbarVisibility = value;
               RaiseStaticPropertyChanged("ProgressbarVisibility");
           }
      }
      public static event EventHandler<PropertyChangedEventArgs> StaticPropertyChanged;
      public static void RaiseStaticPropertyChanged(string propName)
      {
           EventHandler<PropertyChangedEventArgs> handler = StaticPropertyChanged;
           if (handler != null)
               handler(null, new PropertyChangedEventArgs(propName));
     }
}
Run Code Online (Sandbox Code Playgroud)

我这样绑定

var binding = new Binding("ShellWindow.ProgressbarVisibility") { Mode = BindingMode.TwoWay };
       progressbar = new CircularProgressBar ();
  progressbar.SetBinding(VisibilityProperty,
                             binding);
Run Code Online (Sandbox Code Playgroud)

我想我错过了什么,但我不确定我错在哪里.任何帮助都会很棒.

nmc*_*ean 5

文章说使用:

{Binding (local:Repository.Color)}
Run Code Online (Sandbox Code Playgroud)

由于local:在XAML文件之外没有任何意义,我认为不可能用字符串构造绑定.

您也可以指定一个的PropertyPathBinding.Path属性,这种构造的PropertyPath接受PropertyInfo.要使用此构造函数,需要以标记化格式(此处描述)指定路径字符串.从而:

var propertyInfo = typeof(ShellWindow).GetProperty("ProgressbarVisibility");
var propertyPath = new PropertyPath("(0)", propertyInfo);
var binding = new Binding() { Path = propertyPath, Mode = BindingMode.TwoWay };
Run Code Online (Sandbox Code Playgroud)