c# - What's the best way to call INotifyPropertyChanged's PropertyChanged event? -


When you implement the INotifyPropertyChanged interface, you are responsible for making changes to the property by calling the event each time each property Is updated in the category.

This usually leads to the following code:

  Public class Myclass: INotifyPropertyChanged Private bool myfield; Public Bull Myfilled {get {myfield; } Set {if (myfield == value) return; Myfield = Value; OnPropertyChanged (New PropertyChangedEventArgs ("MyField")); }} Private Event PropertyChangedEventHandler PropertyChanged; Private Change On Property Change (Property ChangeEngenseEgages E) {Contract ChangeHandler H = Property Changed; If (h! = Null) h (this, e); }}  

This per asset is 12 lines .

If someone was able to decorate the automatic properties in this way, then it would be so easy:

  [INotifyProperty] Public Double MyFile {get; Set; }  

But unfortunately it is not possible (see on MSDN for example)

How can I reduce the amount of code needed per property?

Actually, this is only 3-4 lines per asset ; Other lines amortized on all "notified" properties:

  class person: INotifyPropertyChanged {#region INotifyPropertyChanged: Shared Bit Public Events PropertyChangedEventHandler PropertyChanged; Private Zero OnPropertyChanged (PropertyChangedEventArgs E) {If (PropertyChanged! = Null) property changed (this, e); } #thendrian private string _firstName; Public string first name {back {back _firstName; } Set {if (_firstName == value) returns; _firstName = value; OnPropertyChanged (New PropertyChangedEventArgs ("FirstName")); }} // Delete other properties too  

You can try something like the following, which shares a lot of weight:

  private String_firstName; Public string first name {back {back _firstName; } Set {SetNotifyingProperty ("First name", ref first, name); }} Private Zero SetNotifyingProperty & lt; T & gt; (String propertyName, ref T field, T value) {if (value.Equals (field)) return; Field = values; OnPropertyChanged (New PropertyChangedEventArgs (propertyName)); }  

Comments