Visual Studio Code Snippet for a Notify Property (INotifyPropertyChanged)

updated version can be found here:

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.


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


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.



Keir Davis said…
A much better alternative to the built-in snippet manager in Visual Studio is Code Barrel. It's free and has an excellent Visual Studio plugin.
Keir Davis said…
This comment has been removed by the author.
Aaron Hoffman said…
Thanks Keir. I'll check out Code Barrel and see what it can do. I'd love to have an easier way to share Code Snippets.

Popular posts from this blog

Search iPhone Text Messages with SQLite SQL Query

Use Azure Functions to Execute a SQL Azure Stored Procedure on a Timer

How to Turn Off Microsoft Arc Touch Mouse Scroll Sound Vibration