Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simplest way to achieve automatic notification of property change

I know that there are solutions out there for implementing INotifyPropertyChanged, but none of them are as simple as: reference this library, create/add this attribute, done (I'm thinking Aspect-Oriented Programming here). Does anyone know of a really simple way to do this? Bonus points if the solution is free.

Here are some relevant links (none of which provide a simple enough answer):

  • Aspect Examples (INotifyPropertyChanged via aspects)
  • LinFu
  • INotifyPropertyChanged auto wiring or how to get rid of redundant code
  • INotifyPropertyChanged With Unity Interception AOP
like image 754
Pat Avatar asked Jan 17 '11 18:01

Pat


People also ask

How do you implement property change?

To implement INotifyPropertyChanged you need to declare the PropertyChanged event and create the OnPropertyChanged method. Then for each property you want change notifications for, you call OnPropertyChanged whenever the property is updated.

How will an object be notified if the property bound to it has been changed?

Remarks. The INotifyPropertyChanged interface is used to notify clients, typically binding clients, that a property value has changed. For example, consider a Person object with a property called FirstName .

What does Raisepropertychanged do?

The RaisePropertyChanging event is used to notify UI or bound elements that the data has changed. For example a TextBox needs to receive a notification when the underlying data changes, so that it can update the text you see in the UI.


2 Answers

Try this https://github.com/Fody/PropertyChanged

It will weave all properties of types that implement INotifyPropertyChanged and even handles dependencies.

Your Code

public class Person : INotifyPropertyChanged {     public event PropertyChangedEventHandler PropertyChanged;      public string GivenNames { get; set; }     public string FamilyName { get; set; }      public string FullName     {         get         {             return string.Format("{0} {1}", GivenNames, FamilyName);         }     }  } 

What gets compiled

public class Person : INotifyPropertyChanged {      public event PropertyChangedEventHandler PropertyChanged;      private string givenNames;     public string GivenNames     {         get { return givenNames; }         set         {             if (value != givenNames)             {                 givenNames = value;                 OnPropertyChanged("GivenNames");                 OnPropertyChanged("FullName");             }         }     }      private string familyName;     public string FamilyName     {         get { return familyName; }         set          {             if (value != familyName)             {                 familyName = value;                 OnPropertyChanged("FamilyName");                 OnPropertyChanged("FullName");             }         }     }      public string FullName     {         get         {             return string.Format("{0} {1}", GivenNames, FamilyName);         }     }          public void OnPropertyChanged(string propertyName)     {         var propertyChanged = PropertyChanged;         if (propertyChanged != null)         {             propertyChanged(this, new PropertyChangedEventArgs(propertyName));             }     } } 

Or you can use attributes for more fine grained control.

like image 84
Simon Avatar answered Oct 09 '22 22:10

Simon


Here is an article showing how to handle this via PostSharp.

like image 38
Reed Copsey Avatar answered Oct 09 '22 23:10

Reed Copsey