WPF 4.5绑定到静态属性

时间:2014-02-12 14:36:08

标签: 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));
     }
    }

我在后面的代码中创建一个控件,并希望将它与此属性绑定。 目前我正在这样做

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

此绑定无效。 我想要遵循这个article,但我没有得到我做错的地方。

1 个答案:

答案 0 :(得分:2)

在.NET 4.5中,您可以向WPF通知对静态属性的更改,但是我认为它与您正常处理更改的属性的方式不同。您必须为每个可以通知更改的静态属性创建事件。该事件必须使用属性名称标题,并且后缀为PropertyChanged。

我发现这篇文章可以帮到你:http://10rem.net/blog/2011/11/29/wpf-45-binding-and-change-notification-for-static-properties