One of the most common tasks you have to do with WPF, when you are plumbing data into your controls is implementing INotifyPropertyChanged.

There have been many attempts at abstracting this away or creating wrappers, but I personally like having the detailed, standard code with getters and setters and method call,, because I am forever adding extra functionality and business logic in those setters.

If you are building a proper sized project to an MVVM pattern, you have probably created a base class, commonly called ViewModelBase, which holds and calls the PropertyChanged event, then you just derive all other ViewModels from that.

using System.ComponentModel;
 
namespace WpfApplication51
{
    class ViewModelBase : INotifyPropertyChanged
    {
    }
}

When you implement the interface, all you get is the event:

using System.ComponentModel;
 
namespace WpfApplication51
{
    class ViewModelBase : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
    }
}

I guess from here on, it's really down to how you use the interface, but one of the most common ways is to call the event from the setter of your property:

string _FirstName;
public string FirstName
{
    get
    {
        return _FirstName;
    }
    set
    {
        if (_FirstName != value)
        {
            _FirstName = value;
            RaisePropertyChanged("FirstName");
        }
    }
}

This is exactly what the "propn" snippet does for you.

Wherever you have implemented INPC, you will therefore also need to add this method:
using System.ComponentModel;
 
namespace WpfApplication51
{
    class ViewModelBase : INotifyPropertyChanged
    {
        void RaisePropertyChanged(string prop)
        {
            if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs(prop)); }
        }
        public event PropertyChangedEventHandler PropertyChanged;
    }
}

That is exactly what the "raisepc" snippet generates.

Throughout your development you will be forever creating public properties to bind to the View.
One very common task is therefore to create these snippets.
Especially if like me, you post a lot of sample code in response to MSDN forums questions.

If you are new to Visual Studio or self taught and didn;t realise the power of snippets, I strongly advise you check for some of the commonly used ones, and collect some of your own that match your code requirements. For me, prop, propn and raisepc are very commonly used.

More about Visual Studio Snippets


If you're not familiar with Visual Studio snippets, just type "prop" and you will see a list of property related snippets:



If you then click the Tab key, you will enter snippet mode as shown below:



In just a few keystrokes you can reduce the amount of work you have to type.

You will notice in the image above, once our propn snippet is added, it is listed along-side the others.

Type "propn" (without the quotes), press Tab and you get:



As you update the values, the corresponding property type, backing field, and property name and RaisePropertyChanged parameter will be completed for you.

There are other examples around, but once you get used to using one, you can generate a lot of tedious code in a few keystrokes.


Download these essential snippets here : http://code.msdn.microsoft.com/WPF-INotifyPropertyChanged-8b63ad72