使用INotifyPropertyChanged的静态属性。 C# [英] Static property using INotifyPropertyChanged. C#

查看:87
本文介绍了使用INotifyPropertyChanged的静态属性。 C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个静态属性,其中 INotifyPropertyChanged 将更新对 DataGrid <$ c $所做的任何更改c>我要绑定的组合框。

I am trying create a static property where INotifyPropertyChanged will update any changes made to a DataGrid ComboBox that I am binding to.

我遇到此错误,

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

Error CS0026 Keyword 'this' is not valid in a static property, static method, or static field

通过搜索,我发现了这个,但是即使经过所有步骤,我仍然不知道如何使它起作用。

Through my searches I came upon this Why can't you use the keyword 'this' in a static method in .Net?, but even after going through everything I still can't figure out how to get this to work.

但是,我所做的任何更改仅使我无法尝试使用 INotifyPropertyChanged 来创建静态属性?

But, anything I change only negates that I am trying to make a static property with 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));
    }

下面的代码是我如何将属性更改处理程序用于非静态属性,

And the below code is how I am using the property changed handler for non static properties,

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 this blog post for details about static property change notification.

这篇关于使用INotifyPropertyChanged的静态属性。 C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆