Mohd Ahmed
Mohd Ahmed

Reputation: 1482

WPF 4.5 Binding to static properties

I have a static property in my class like

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

I am creating a control in code behind and wanted to bind it with this property. Currently i am doing like this

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

This binding is not working. I am tring to follow this article but i didn't get where i am doing wrong.

Upvotes: 2

Views: 1154

Answers (1)

deloreyk
deloreyk

Reputation: 1239

In .NET 4.5 you can notify WPF of a change to a static property, however I believe it is different than how you normally handle a property changed. You must create event for each static property that can be notified of a change. The event must be titled with the property name, and have a suffix of PropertyChanged.

I found this article that may help you out: http://10rem.net/blog/2011/11/29/wpf-45-binding-and-change-notification-for-static-properties

Upvotes: 1

Related Questions