Posts
114
Comments
130
Trackbacks
10
INotifyPropertyChanged with less typing using a Code Snippet

 

Kevin Grossnicklaus gave a nice presentation on M-V-VM User Interface Patterns in Silverlight and WPF at last night's St. Louis .NET User Group meeting.

A key part of MVVM is implementation of the INotifyPropertyChanged interface to let views know that bound properties have changed. Here's a simplified version of a model class from the MSDN INotifyPropertyChanged documentation:

public class DemoCustomer : INotifyPropertyChanged
{
    private Guid _id = Guid.NewGuid();
 
    public Guid ID
    {
        get { return _id; }
    }
 
    private string _customerName;
    public string CustomerName
    {
        get { return _customerName; }
 
        set
        {
            if (value != _customerName)
            {
                _customerName = value;
                NotifyPropertyChanged("CustomerName");
            }
        }
    }
 
    private string _phoneNumber;
    public string PhoneNumber
    {
        get { return _phoneNumber; }
 
        set
        {
            if (value != _phoneNumber)
            {
                _phoneNumber = value;
                NotifyPropertyChanged("PhoneNumber");
            }
        }
    }
 
    public event PropertyChangedEventHandler PropertyChanged;
 
    private void NotifyPropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    }
}

 

That's a lot of repetitive typing for each of the notifying properties. Creating the properties can be simplified by creating a Visual Studio code snippet. I have one called "propn" for properties with notification:

<?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 property and backing field in class implementing INotifyPropertyChanged</Description>
            <Author>Brian Schroer</Author>
            <SnippetTypes>
                <SnippetType>Expansion</SnippetType>
            </SnippetTypes>
        </Header>
        <Snippet>
            <Declarations>
                <Literal>
                    <ID>type</ID>
                    <ToolTip>Property type</ToolTip>
                    <Default>string</Default>
                </Literal>
                <Literal>
                    <ID>property</ID>
                    <ToolTip>Property name</ToolTip>
                    <Default>MyProperty</Default>
                </Literal>
        <Literal>
          <ID>notifyMethod</ID>
          <ToolTip>name of method to raise PropertyChanged event</ToolTip>
          <Default>NotifyPropertyChanged</Default>
        </Literal>
      </Declarations>
            <Code Language="csharp"><![CDATA[private $type$ _$property$;
    public $type$ $property$
    {
        get { return _$property$;}
        set 
    { 
        if (value != _$property$)
        {
            _$property$ = value;
            $notifyMethod$("$property$");
        }
    }
    }
    $end$]]>
            </Code>
        </Snippet>
    </CodeSnippet>
</CodeSnippets>

 

…so I can just type "propn" and hit tab-tab to display the snippet, with updatable fields for the property type and name. (The name of the method called to raise the PropertyChanged event is also updatable - You don't have to call it "NotifyPropertyChanged"):

If you haven't created your own code snippets before, you may be pleasantly surprised to find out how easy it is. There are plenty of built-in snippets to use as a starting point. Taking the time to build a snippet will pay off with a lot of time saved coding.

In my next post, I'll talk about how you can use generics and reflection to reduce the amount of code required for implementing "notifiable" properties.

posted on Tuesday, July 27, 2010 7:48 AM Print
Comments
Gravatar
# re: INotifyPropertyChanged with less typing using a Code Snippet
Mark Borcherding
7/27/2010 7:59 AM
That's a great idea. I was thinking that looked like a pain typing all that when it is mostly the same. I was wondering if there was some AOP feature that would let you simply attribute your property and get the notification.
Gravatar
# re: INotifyPropertyChanged with less typing using a Code Snippet
Ram
3/5/2012 9:23 AM
Thanks for the help

I liked it!! and using it
Gravatar
# re: INotifyPropertyChanged with less typing using a Code Snippet
David
4/21/2012 6:14 PM
Big thumbs up!! Thanks.
Gravatar
# re: INotifyPropertyChanged OnProperyChanged event genration
Prabu
6/3/2013 3:31 AM
Can u tell me how to generate Inotify property change event by short cut key.. and for ICommand also.. thank u!!
Gravatar
# re: INotifyPropertyChanged with less typing using a Code Snippet
mosby
8/15/2013 3:10 AM
nice & nice
Gravatar
# re: INotifyPropertyChanged with less typing using a Code Snippet
Sergei
8/24/2013 2:01 PM
Try Property Builder:
http://propertiesbuilder.codeplex.com/

Post Comment

Title *
Name *
Email
Comment *  
 
Tag Cloud