INotifyPropertyChanged for static variable

13,826

Solution 1

INotifyPropertyChanged works on instance properties. One solution is to use a singleton pattern and keep INotifyPropertyChanged, the other is to use your own event to notify listeners.

Singleton example

public sealed class MyClass: INotifyPropertyChanged
{
   private static readonly MyClass instance = new MyClass();
   private MyClass() {}

   public static MyClass Instance
   {
      get 
      {
         return instance; 
      }
   }

   // notifying property
   private string privMyProp;
   public string MyProp
   {
       get { return this.privMyProp; }

       set
       {
           if (value != this.privMyProp)
           {
               this.privMyProp = value;
               NotifyPropertyChanged("MyProp");
           }
       }
   }


   // INotifyPropertyChanged implementation
   public event PropertyChangedEventHandler PropertyChanged;

   private void NotifyPropertyChanged(String info)
   {
       var handler = PropertyChanged;
       if (handler != null)
       {
           handler(this, new PropertyChangedEventArgs(info));
       }
   }
}

EDIT: In WPF 4.5, they introduced property changed mechanic for static properties:

You can use static properties as the source of a data binding. The data binding engine recognizes when the property's value changes if a static event is raised. For example, if the class SomeClass defines a static property called MyProperty, SomeClass can define a static event that is raised when the value of MyProperty changes. The static event can use either of the following signatures.

public static event EventHandler MyPropertyChanged;
public static event EventHandler<PropertyChangedEventArgs> StaticPropertyChanged;

Solution 2

Very good example, I used it for some general settings in application, when I want to bind some property online to components

    public sealed class DataGridClass:INotifyPropertyChanged
{
      private static readonly DataGridClass instance = new DataGridClass();
      private DataGridClass() { }

      public static DataGridClass Instance
       {
          get 
          {
             return instance; 
          }
       }

    private int _DataGridFontSize {get;set;}

    public int DataGridFontSize
    {
        get { return _DataGridFontSize; }
        set { _DataGridFontSize = value;
            RaisePropertyChanged("DataGridFontSize");
        }
    }
    public event PropertyChangedEventHandler PropertyChanged;

    private void RaisePropertyChanged(string name)
    {
        if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(name));
    }

Set startup properties:

DataGridClass.Instance.DataGridFontSize = 14(or read from xml)

Bind this to components properties

xmlns:static="clr-namespace:MyProject.Static"
<extgrid:ExtendedDataGrid x:Name="testGrid"  ClipboardCopyMode="IncludeHeader" AutoGenerateColumns="False">
<extgrid:ExtendedDataGrid.Resources>
    <Style TargetType="{x:Type DataGridCell}">
        <Setter Property="FontSize" 
        Value="{Binding Source={x:Static static:DataGridClass.Instance},
        Path=DataGridFontSize, Mode=OneWay,UpdateSourceTrigger=PropertyChanged}"/>
    </Style>
</extgrid:ExtendedDataGrid.Resources>

When you change this value somewhere in application like "Preferences"->DataGrid FontSize - to it automatically update this property for bindings with UpdateSourceTrigger

    private void comboBoxFontSize_DropDownClosed(object sender, EventArgs e)
    {
        DataGridClass.Instance.DataGridFontSize = Convert.ToInt32(comboBoxFontSize.Text);
    }


   <ComboBox Grid.Column="1" Grid.Row="0" Height="21" Width="75" Name="comboBoxFontSize" HorizontalAlignment="Left" 
                              VerticalAlignment="Center"  DropDownClosed="comboBoxFontSize_DropDownClosed" 
                              ItemsSource="{Binding Source={x:Static commands:ConstClass.ListOfFontSize},Mode=OneWay}" 
                              SelectedItem="{Binding Source={x:Static static:DataGridClass.Instance},Path=DataGridFontSize, 
                        Mode=OneWay,UpdateSourceTrigger=PropertyChanged}"/>
Share:
13,826
Shibli
Author by

Shibli

-

Updated on July 27, 2022

Comments

  • Shibli
    Shibli almost 2 years

    I had a variable which was not static and INotifyPropertyChanged implemented succesfully. Then I tried to make it global, so turned it a static variable. But this time, INotifyPropertyChanged does not work. Any solution?

  • chopikadze
    chopikadze about 12 years
    @Shilbi A small example of what?
  • RobHurd
    RobHurd almost 11 years
    Thanks very much for this example, it's exactly what I needed. It works a treat!
  • wildeyes
    wildeyes over 8 years
    Where can I read more about WPF 4.5 and static property binding?
  • C. Tewalt
    C. Tewalt about 8 years
    @AgafadaEm The links are all down, but I found the arvchived page on web archive.org here. web.archive.org/web/20131129053934/http://msdn.microsoft.com‌​/…
  • Dr. Andrew Burnett-Thompson
    Dr. Andrew Burnett-Thompson over 5 years
    Careful, I just debugged a memory leak related to binding to x:Static singletons. Now experimenting with the .NET4.5 method above
  • Berger
    Berger over 4 years
    a good working example can be found here it's worth to mention that public static event PropertyChangedEventHandler StaticPropertyChanged; can not be inherited and should thereby be in the class that is calling it.