While developing WPF/Silverlight applications, and more specifically while following the Model-View-ViewModel (MVVM) pattern we will find ourselves implementing INotifyProprertyChanged most of the times. The default implementation of PropertyChanged event takes the property name as string in the PropertyChangedEventArgs which is not much robust. There are several ways to address the issue:
- Use reflection to verify that the property actually exists, as demonstrated once by Josh Smith.
- Use Injection using some Aspect Oriented Programming framework, like PostSharp
- Use Expression Trees as described by Michael Sync and Davy Brion
Personally, I prefer using expression trees. So, instead of writing this:
public string MyProperty
{
get { return this.myProperty; }
set { this.myProperty = value; this.RaisePropertyChanged("MyProperty"); }
}
We can write:
public string MyProperty
{
get { return this.myProperty; }
set { this.myProperty = value; this.RaisePropertyChanged( MyObj => MyObj.MyProperty ); }
}
The same issue exists looking at the other side. When we subscribe to PropertyChanged event of an object, we get the property name again as a string. One way is to use the GetPropertyName( ExpressionTree ) extension method from the above implementation in our if and case statements. Also, Josh nicely addressed the issue in this post, thus allowing us to write:
MyClass myObject = new MyClass();
PropertyObserver<MyClass> observer = new PropertyObserver<MyClass>(myObject)
.RegisterHandler(myObj => myObj.MyProperty1, myObj => { /* handle change in MyProperty1 */ })
.RegisterHandler(myObj => myObj.MyProperty2, MyProperty2HandlerMethod );
Notice that Josh used IWeakEventListener that isn’t available for Silverlight but luckily Pete O’ Hanlon provided us with a Silverlight version of Josh’s work here.
So, combining the great efforts of all these people, we are going to have a better MVVM experience.






