Binding to static class property

Viewed 45444

I want to bind a textblock text to a property of a static class. Whenever the property value of the static class changes, it should reflect to the textblock which is on the other window or custom control.

4 Answers

It worked for me!

When you have static class with static property like this

 namespace App.Classes
 {
     public static class AppData
     {
         private static ConfigModel _configModel;
         public static ConfigModel Configuration
         {
            get { return _configModel; }
            set { _configModel = value; }
         }
     }

     public class ConfigModel : INotifyPropertyChanged
     {
         public event PropertyChangedEventHandler PropertyChanged;

          private bool _text = true;
          public bool Text
          {
               get { return _text ; }
               set { 
                     _text = value; 
                     PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("Text"));
               }
          }
      }
}

You can bind it to xaml like this.

xmlns:c="clr-namespace:App.Classes"

<TextBlock Text="{Binding Path=Text, Source={x:Static c:AppData.Configuration}}"/>
Related