Question

I find myself doing this all the time:

public int Number 
{
get{return _Number;} 
set{_Number = value; NotifyPropertyChanged("Number");}
}

private int _Number;

And tons of these variables in many many classes. Is there any IDE or tool or addon that helps auto generate the private variables and the Notifiy portion?

Any ideas? Thanks.

Was it helpful?

Solution

Yeah, they're called code snippets.

Interestingly, I happen to use this snippet quite often, so here is the code:

<?xml version="1.0" encoding="utf-8"?>
<CodeSnippets
    xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
    <CodeSnippet Format="1.0.0">
        <Header>
            <Title>notifyproperty</Title>
            <Shortcut>propnotif</Shortcut>
            <SnippetTypes>
                <SnippetType>Expansion</SnippetType>
            </SnippetTypes>
        </Header>
        <Snippet>
            <Declarations>
                <Literal>
                    <ID>field</ID>
                    <ToolTip>Field name.</ToolTip>
                    <Default>_myField</Default>
                </Literal>
                <Literal>
                    <ID>type</ID>
                    <ToolTip>Type name.</ToolTip>
                    <Default>string</Default>
                </Literal>
                <Literal>
                    <ID>property</ID>
                    <ToolTip>Property name.</ToolTip>
                    <Default>MyProperty</Default>
                </Literal>
            </Declarations>
            <Code Language="csharp">
                <![CDATA[private $type$ $field$;
        public $type$ $property$
        {
            get { return $field$; }
            set 
            {
                $field$ = value; NotifyPropertyChanged("$property$");
            }
        }]]>
            </Code>
        </Snippet>
    </CodeSnippet>
</CodeSnippets>

OTHER TIPS

Resharper provides great tooling for creating INotifyPropertyChanged automatically.

For details, see the JetBrians blog post on INotifyPropertyChanged.

Simple solution. You need to type in prop in Visual Studio and press tab twice and this will give you the skeleton code for the properties.

Detailed Solution Refer to @newStachExchangeInstanc answer

There are a LOAD of code snippets build into the IDE.

prop gives you: public TYPE Name {get;set;}
propdp gives you a full WPF dependency property

And so on. Check this MSDN link out for all of the existing ones. As the previous answer states, you can also create your own.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top