INotifyPropertyChanged for static variable

I had a variable that was not static, and INotifyPropertyChanged implemented successfully. Then I tried to make it global, so I turned it into a static variable. But this time, INotifyPropertyChanged is not working. Any solution?

+6
source share
2 answers

INotifyPropertyChanged works on instance properties. One solution is to use a singleton pattern and save INotifyPropertyChanged , and 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 a modified mechanic property for static properties:

You can use static properties as a source of data binding. the data binding engine recognizes when a property value changes if a static event. For example, if the SomeClass class defines a static property of MyProperty, SomeClass can define a static event that occurs when the value of MyProperty changes. A static event can use any of the following signatures.

 public static event EventHandler MyPropertyChanged; public static event EventHandler<PropertyChangedEventArgs> StaticPropertyChanged; 
+21
source

A very good example, I used it for some general settings in the 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 launch properties:

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

Bind this to component 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 the application, for example, Preferences → DataGrid FontSize, it automatically updates this property for bindings using 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}"/> 
+5
source

Source: https://habr.com/ru/post/911192/


All Articles