Visual Studio snippet for a view model property, using the SetProperty() method


I spent my 3 months summer internship coding a Windows 8 application, and even though there now is a basic implementation of the INotifyPropertyChanged interface provided by Microsoft (BindableBase), I can tell you creating a view model for each view you need is very time consuming and boring, and not nearly as quick as I would like it to be.

Of course, you have the prop and propfull snippets, which is a nice thing, but you still have to modify the setter yourself to use the SetProperty() method, which is a shame.

After a dozen view models, I got very bored, and went looking into creating a snippet…

propnotif illustration

Nice, isn’t it? 🙂

Turns out it’s really easy!

I used the existing propfull snippet, modified it a bit, and voilà !

Here is the entire xml code for the snippet :

<?xml version="1.0" encoding="utf-8" ?>
<CodeSnippets  xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
  <CodeSnippet Format="1.0.0">
      <Description>Code snippet for view model properties, using the SetProperty() method from BindableBase</Description>
      <Author>Melvyn Laily</Author>
          <ToolTip>Property type</ToolTip>
          <ToolTip>Property name</ToolTip>
      <Code Language="csharp">
<![CDATA[private $type$ _$property$;
  public $type$ $property$
    get { return _$property$; }
    set { SetProperty(ref _$property$, value); }

and a download link if you prefer: propnotif.snippet.

Code Snippets Manager

The Code Snippets Manager Window

You can import it in Visual Studio in Tools > Code Snippets Manager...

0 comment

Formatting cheat sheet.
The current page url links to a specific comment.
The comment is shown highlighted below in context.

    JavaScript is required to see the comments. Sorry...