静态变量的INotifyPropertyChanged
本文关键字:INotifyPropertyChanged 变量 静态 | 更新日期: 2023-09-27 18:28:18
我有一个非静态变量,INotifyPropertyChanged成功实现。然后我试着把它变成全局的,所以把它变成了一个静态变量。但这一次,INotifyPropertyChanged不起作用。有什么解决方案吗?
INotifyPropertyChanged
处理实例属性。一种解决方案是使用singleton模式并保留INotifyPropertyChanged
,另一种是使用自己的事件来通知侦听器。
Singleton示例
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:在WPF 4.5中,他们引入了静态属性的属性更改机制:
您可以使用静态属性作为数据绑定的源。这个如果引发静态事件。例如,如果类SomeClass定义了静态属性MyProperty,SomeClass可以定义静态事件当MyProperty的值更改时引发的。静态事件可以使用以下任一签名。
public static event EventHandler MyPropertyChanged;
public static event EventHandler<PropertyChangedEventArgs> StaticPropertyChanged;
非常好的例子,当我想将一些属性在线绑定到组件时,我在应用程序中的一些常规设置中使用了它
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));
}
设置启动属性:
DataGridClass.Instance.DataGridFontSize = 14(or read from xml)
将其绑定到组件属性
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>
当您在应用程序(如"Preferences"->DataGrid FontSize)中的某个位置更改此值时,它会自动更新此属性,以便使用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}"/>