Sunday, September 26, 2010

Visual Studio Code Snippet for a Notify Property (INotifyPropertyChanged)

In the ViewModel classes of projects following the M-V-VM pattern it is often necessary to raise a "PropertyChanged" event (to assist with INotifyPropertyChanged interface implementation) from within a property's setter.  This is a tedious task that will hopefully someday be solved by using the Compiler as a Service...  But until that day comes, I've created a handy Visual Studio Code Snippet for myself to help automate this task.  The XML for the code snippet, and an example of the code it produces are below (note the Base Class and the OnPropertyChanged() method call).  Continue reading to see how to "install" and implement this snippet.

(or download the snippet here: http://aaronhoffman.googlecode.com/files/NotifyPropertyCodeSnippet.zip)

--------

<?xml version= "1.0 " encoding= "utf-8 " ?> 
<CodeSnippets  xmlns= "http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet "> 
  <CodeSnippet Format= "1.0.0 "> 
    <Header> 
      <Title> propn</Title> 
      <Shortcut> propn</Shortcut> 
      <Description> 
Code snippet for a property that calls OnPropertyChanged() in the setter (to assist in INotifyPropertyChanged implementation).
      </Description> 
      <Author> Aaron Hoffman</Author> 
      <SnippetTypes> 
        <SnippetType> Expansion</SnippetType> 
      </SnippetTypes> 
    </Header> 
    <Snippet> 
      <Declarations> 
        <Literal> 
          <ID> type</ID> 
          <ToolTip> Property type</ToolTip> 
          <Default> int</Default> 
        </Literal> 
        <Literal> 
          <ID> property</ID> 
          <ToolTip> Property name</ToolTip> 
          <Default> MyProperty</Default> 
        </Literal> 
      </Declarations> 
      <Code Language= "csharp "> 
        <![CDATA[public $type$ $property$ 
    { 
        get { return _$property$; } 
        set 
        { 
            if (_$property$ != value) 
            { 
                _$property$ = value; 
                OnPropertyChanged($property$PropertyName); 
            } 
        } 
    } 
private $type$ _$property$; 
public const string $property$PropertyName = "$property$";$end$]]> 
      </Code> 
    </Snippet> 
  </CodeSnippet> 
</CodeSnippets>

---------


























Take the XML above and save it in a file named propn.snippet.  This will be referenced later during the import process.

This code snippet relies on a OnPropertyChanged(string propertyName) method already existing.  This is easily done by extending all of your ViewModel classes from a ViewModel Base Class that implements the INotifyPropertyChanged interface, and placing the method there.  Here is a simple example:

--------

  public class ViewModelBase : INotifyPropertyChanged
  {
    public event PropertyChangedEventHandler PropertyChanged;
 
    protected virtual void OnPropertyChanged(string propertyName)
    {
      var eh = this.PropertyChanged;
 
      if (eh != null)
        eh(thisnew PropertyChangedEventArgs(propertyName));
    }
  }

--------


To "Install" the code snippet, select "Code Snippets Manager..." from the Tools menu.
























When the Code Snippet Manager window opens, make sure the Language is set to C# and click the Import... Button.





















Find where you created and saved the XML above into a file named propn.snippet, and select that file to import.  The Import Code Snippet window will open.  See that the "My Code Snippets" folder is selected and click the Finish button.





















Click OK on the Code Snippet Manager window.  Now within any C# file you should be able to start typing "propn" then Tab and Tab and the code snippet should be inserted into the file.


-