我正在尝试创建一个静态属性,其中INotifyPropertyChanged
将更新DataGrid
ComboBox
对我绑定的任何更改.
我收到这个错误,
错误CS0026关键字'this'在静态属性,静态方法或静态字段中无效
通过我的搜索,我发现了为什么你不能在.Net的静态方法中使用关键字'this'?,但即使经历了一切,我仍然无法弄清楚如何让这个工作.
但是,我改变的任何东西都否定了我试图用INotifyPropertyChanged
??? 创建一个静态属性?
我的代码:
private static List<string> _nursingHomeSectionListProperty;
public static List<string> NursingHomeSectionListProperty
{
get { return _nursingHomeSectionListProperty; }
set
{
_nursingHomeSectionListProperty = value;
NotifyStaticPropertyChanged();
}
}
Run Code Online (Sandbox Code Playgroud)
而Property改变了处理程序
public static event PropertyChangedEventHandler StaticPropertyChanged;
public static void NotifyStaticPropertyChanged([CallerMemberName] string propertyName = null)
{
StaticPropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
Run Code Online (Sandbox Code Playgroud)
以下代码是我如何使用属性更改处理程序的非静态属性,
public event PropertyChangedEventHandler PropertyChanged;
public void NotifyPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
Run Code Online (Sandbox Code Playgroud)
只是通过null
而不是this
:
public static event PropertyChangedEventHandler StaticPropertyChanged;
private static void NotifyStaticPropertyChanged([CallerMemberName] string name = null)
{
StaticPropertyChanged?.Invoke(null, new PropertyChangedEventArgs(name));
}
Run Code Online (Sandbox Code Playgroud)
有关静态属性更改通知的详细信息,请参阅此博客文章
归档时间: |
|
查看次数: |
2375 次 |
最近记录: |