我正在尝试创建一个静态属性,其中INotifyPropertyChanged
将更新对 a 所做的任何更改DataGrid
ComboBox
我有约束力。
我收到这个错误,
错误 CS0026 关键字“this”在静态属性、静态中无效
方法或静态字段
通过我的搜索我发现了这个为什么不能在 .Net 的静态方法中使用关键字“this”? https://stackoverflow.com/questions/134237/why-cant-you-use-the-keyword-this-in-a-static-method-in-net,但即使经历了一切之后我仍然不知道如何让它发挥作用。
但是,我所做的任何更改都只会否定我试图使用以下方法创建静态属性INotifyPropertyChanged
???
我的代码:
private static List<string> _nursingHomeSectionListProperty;
public static List<string> NursingHomeSectionListProperty
{
get { return _nursingHomeSectionListProperty; }
set
{
_nursingHomeSectionListProperty = value;
NotifyStaticPropertyChanged();
}
}
并且属性更改了处理程序
public static event PropertyChangedEventHandler StaticPropertyChanged;
public static void NotifyStaticPropertyChanged([CallerMemberName] string propertyName = null)
{
StaticPropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
下面的代码是我如何使用非静态属性的属性更改处理程序,
public event PropertyChangedEventHandler PropertyChanged;
public void NotifyPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
只要通过null
代替this
:
public static event PropertyChangedEventHandler StaticPropertyChanged;
private static void NotifyStaticPropertyChanged([CallerMemberName] string name = null)
{
StaticPropertyChanged?.Invoke(null, new PropertyChangedEventArgs(name));
}
See 这篇博文 http://10rem.net/blog/2011/11/29/wpf-45-binding-and-change-notification-for-static-properties有关静态属性更改通知的详细信息。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)