简体   繁体   中英

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).

Is there any way to raise PropertyChanged on All the properties without having to manually go through and pluck it in the 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.

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

public Type Name {get; set; notify { () => 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.

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.

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,

which will implement INotifyPropertyChanged,

You register in its Constructor to the PropertyChanged event and check via reflection if the property that changed has your attribute on it,

and then set IsDirty accordingly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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