【问题标题】:Binding static property in code behind在后面的代码中绑定静态属性
【发布时间】:2014-02-17 12:26:37
【问题描述】:

我正在尝试关注这个article,唯一的区别是我在后面的代码中创建和绑定控件。 不幸的是,它不起作用。这是我的示例代码:

 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));
     }
}

我是这样绑定的

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

我想我遗漏了一些东西,但我不确定我错在哪里。 任何帮助都会很棒。

【问题讨论】:

    标签: c# wpf xaml binding


    【解决方案1】:

    文章说要使用:

    {Binding (local:Repository.Color)}
    

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

    您还可以将PropertyPath 分配给Binding.Path 属性,并且this PropertyPath constructor 接受PropertyInfo。要使用此构造函数,需要以标记化格式指定路径字符串(描述为here)。因此:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-02-25
      • 1970-01-01
      • 2013-09-17
      • 2011-08-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多