使用INotifyPropertyChanged的静态属性。 C#

时间:2017-01-07 08:21:04

标签: c# wpf

我正在尝试创建一个静态属性,其中INotifyPropertyChanged将更新对我绑定的DataGrid ComboBox所做的任何更改。

我收到此错误,

  

错误CS0026关键字'this'在静态属性static中无效   方法或静态字段

通过我的搜索,我发现了Why can't 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();
    }
}

并且Property更改了处理程序

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));
   }

1 个答案:

答案 0 :(得分:8)

只需传递null而不是this

public static event PropertyChangedEventHandler StaticPropertyChanged;

private static void NotifyStaticPropertyChanged([CallerMemberName] string name = null)
{
    StaticPropertyChanged?.Invoke(null, new PropertyChangedEventArgs(name));
}

有关静态属性更改通知的详细信息,请参阅this blog post