3

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.

Manu Andrei
  • 62
  • 1
  • 15
  • 1
    You can T4 generate the properties - you just need a text-file containing all your properties (could be CSV, XML, JSON, whatever) and a T4 script to turn that into a class. Lots of examples on the internet :) – RB. May 08 '17 at 16:15
  • Usually you can just pass an empty string as the property name depending on what is subscribed – TyCobb May 08 '17 at 16:15
  • 2
    Possible duplicate of [Implementing INotifyPropertyChanged - does a better way exist?](http://stackoverflow.com/questions/1315621/implementing-inotifypropertychanged-does-a-better-way-exist) – TheLethalCoder May 08 '17 at 16:16
  • The other alternative is Aspect Oriented Programming, for example https://code.google.com/archive/p/propfu/ – RB. May 08 '17 at 16:18

2 Answers2

6

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

Jonathan Allen
  • 68,373
  • 70
  • 259
  • 447
-1

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.

Mishka
  • 508
  • 3
  • 5