简体   繁体   English

所有属性上的 INotifyPropertyChanged

[英]INotifyPropertyChanged on all properties

Considering a class with a large number of properties, I want to implement a Dirty flag (in order to know if I should update the value in the database).考虑到一个具有大量属性的类,我想实现一个 Dirty 标志(为了知道我是否应该更新数据库中的值)。

Is there any way to raise PropertyChanged on All the properties without having to manually go through and pluck it in the setter?有没有办法在所有PropertyChanged上提高PropertyChanged ,而不必手动通过并在 setter 中提取它?

Edit to clear up some things: I did go through the thread that was linked here, and found some things, but unfortunately they do not suit my need.编辑以澄清一些事情:我确实浏览了此处链接的线程,并找到了一些东西,但不幸的是它们不适合我的需要。 I don't really need to send an event, I just need to flip a flag.我真的不需要发送事件,我只需要翻转一个标志。 Also, that thread is quite old, and I hoped that maybe with C# 7 something came out which would help with it, that I missed in the changelog.此外,该线程已经很老了,我希望可能在 C# 7 中出现一些可以帮助它的东西,我在更新日志中错过了它。

Why don't I just go and do it manually?为什么我不去手动做呢? Well, I might have to.好吧,我可能不得不这样做。 But I'd have to declare the "hidden" variables, manage the code myself, I hoped MS would've done something to help, maybe something like was suggested in the other topic但是我必须声明“隐藏”变量,自己管理代码,我希望 MS 会做一些帮助,也许像其他主题中建议的那样

public Type Name {get;公共类型名称 {get; set;放; notify { () => IsDirty = true;通知 { () => IsDirty = true; }} }}

that would help a lot (ignoring the fact it would ask me to declare the get and set anyways because they're abstract.这会很有帮助(忽略它会要求我声明 get 和 set 的事实,因为它们是抽象的。

Add a method that looks like this:添加一个如下所示的方法:

public void Test()
{
     if(PropertyChanged != null)
         PropertyChanged(new PropertyChangedEventArgs(null));
}

Passing a null or empty string as the property name tells consumers that all properties have been changed.传递 null 或空字符串作为属性名称告诉消费者所有属性都已更改。

https://msdn.microsoft.com/en-us/library/system.componentmodel.propertychangedeventargs.propertyname(v=vs.110).aspx https://msdn.microsoft.com/en-us/library/system.componentmodel.propertychangedeventargs.propertyname(v=vs.110).aspx

You can but its also a lot of work.你可以,但它也有很多工作。 Make an Attribute and use it on those properties.创建一个属性并在这些属性上使用它。 in the base class of your ViewModel,在 ViewModel 的基类中,

which will implement INotifyPropertyChanged,这将实现 INotifyPropertyChanged,

You register in its Constructor to the PropertyChanged event and check via reflection if the property that changed has your attribute on it,您在其构造函数中注册到 PropertyChanged 事件并通过反射检查更改的属性是否具有您的属性,

and then set IsDirty accordingly.然后相应地设置 IsDirty。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM