Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

INotifyPropertyChanged on all properties

Tags:

c#

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.

like image 988
Manu Andrei Avatar asked Sep 15 '25 10:09

Manu Andrei


1 Answers

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

like image 151
Jonathan Allen Avatar answered Sep 18 '25 09:09

Jonathan Allen



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!