Geeks With Blogs

@BrianSchroer
  • BrianSchroer I see a lot of fans wearing #stlcards red at the Cincinnati ballpark. ;) about 561 days ago
  • BrianSchroer What part of "What part of x do you not understand" do you think is clever? about 561 days ago
  • BrianSchroer Anonymous notes to those with whom I shared a conference call today: 1) The "t" is silent in "across". 2) "Uniquer" is not a word. about 564 days ago
  • BrianSchroer KSDK: "Don't take any chances. Get in the basement now. Now let's hear from our reporter, driving up I-64 TOWARD THE STORM." about 569 days ago
  • BrianSchroer Congratulations to Joanna Schroer & University City High School Class of '13 #UTime about 569 days ago
  • BrianSchroer Ugh. McCarver doing the #stlcards game. This guy makes Hrabosky seem witty and concise by comparison. about 575 days ago

News

Brian Schroer Don't Call Me Mort!

 

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 .NET | Back to top


Comments on this post: INotifyPropertyChanged with less typing using a Code Snippet

# re: INotifyPropertyChanged with less typing using a Code Snippet
Requesting Gravatar...
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.
Left by Mark Borcherding on Jul 27, 2010 7:59 AM

# re: INotifyPropertyChanged with less typing using a Code Snippet
Requesting Gravatar...
Thanks for the help

I liked it!! and using it
Left by Ram on Mar 05, 2012 9:23 AM

# re: INotifyPropertyChanged with less typing using a Code Snippet
Requesting Gravatar...
Big thumbs up!! Thanks.
Left by David on Apr 21, 2012 6:14 PM

# re: INotifyPropertyChanged OnProperyChanged event genration
Requesting Gravatar...
Can u tell me how to generate Inotify property change event by short cut key.. and for ICommand also.. thank u!!
Left by Prabu on Jun 03, 2013 3:31 AM

# re: INotifyPropertyChanged with less typing using a Code Snippet
Requesting Gravatar...
nice & nice
Left by mosby on Aug 15, 2013 3:10 AM

# re: INotifyPropertyChanged with less typing using a Code Snippet
Requesting Gravatar...
Try Property Builder:
http://propertiesbuilder.codeplex.com/
Left by Sergei on Aug 24, 2013 2:01 PM

Your comment:
 (will show your gravatar)


Copyright © Brian Schroer | Powered by: GeeksWithBlogs.net | Join free