(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(this, new 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.
-



