Sample ViewModelBase w/ INotifyPropertyChanged support
A sample for both magic string and expression based OnProperyChanged() methods.
Sample:
public class ViewModelBase : INotifyPropertyChanged { public event PropertyChangedEventHandler PropertyChanged = delegate { }; public void OnPropertyChanged(String propertyName) { var handler = PropertyChanged; if ( handler != null ) handler(this, new PropertyChangedEventArgs(propertyName)); } public void OnPropertyChanged<TProperty>(Expression<Func<TProperty>> property) { var lambda = (LambdaExpression)property; MemberExpression memberExpression; if ( lambda.Body is UnaryExpression ) { var unaryExpression = (UnaryExpression)lambda.Body; memberExpression = (MemberExpression)unaryExpression.Operand; } else { memberExpression = (MemberExpression)lambda.Body; } OnPropertyChanged(memberExpression.Member.Name); } }
Usages:
private string name; public string Name { get { return name; } set { if ( name == value ) return; name = value; OnPropertyChanged("Name"); // Magic-string. Not the best approach! OnPropertyChanged(() => Name); // Type-safe. Safe for refactoring too. } }
You’ll notice my usage sample fires the OnPropertyChanged method twice. Pick one. Firing it twice both raise the event twice.
Recommendation:
I use the shown ViewModelBase in most of my projects. (Not every problem is a nail. Not every solution requires a hammer.)
It is much easier to write and maintain my code with the expression-style method. No more magic strings to catch me out after refactoring. No more guessing at what something was called or struggling with typos either. It works and it makes my life easier.
Keep in mind the expression-style method is a wrapper for the magic string-style. In this sample, the string version is required to support the expression version.
Alternative:
Create a NotifyPropertyChanged class, then have ViewModelBase use NotifyPropertyChanged as a base class. This approach allows you to use NotifyPropertyChanged in more situations. Keep it DRY.
Now playing:
Judas Priest
Victim of Changes
Metal Works ‘73 – ‘93 (1 of 2)

Hello and thanks for this great article.
I’m new to MVVM and I don’t know if the right way is to always redeclare all the properties of the Model into the ViewMode, is it? or is there a ViewModelBase that is automatically aware of the fields in the Model?
Please view my question here too: http://stackoverflow.com/questions/4917966/dynamic-generic-viewmodelbase
@Shimmy
Like with most programming problems… It all depends.
I had experience with MVP in a WinForms context before I started working with WPF/MVVM. For me the key was to understand the layers and how they work together.
Take a look at this graphic from Karl Shifflett’s web site. Karl has a bunch of code you can look at.
Here are a few references…
http://karlshifflett.wordpress.com/mvvm/
http://msdn.microsoft.com/en-us/magazine/dd419663.aspx
http://joshsmithonwpf.wordpress.com/advanced-mvvm/
http://blogs.msdn.com/b/johngossman/archive/2005/10/08/478683.aspx