There are two ways to sort of do what you are asking. The first (.NET 4.5+ only!) way to do it is to modify OnPropertyChanged to:
private void OnPropertyChanged([CallerMemberName] string propertyName = "")
{
....
}
Then you call it from the setter:
set
{
_var = value;
OnPropertyChanged();
}
This will automatically populate the "propertyName" argument with the calling property, unless you provide the argument explicitly. Note you need a using System.Runtime.CompilerServices;
to get the namespace for the CallerMemberName
attribute.
The other way would be to write a custom version of the propfull
snippet to populate it for you at creation time. My version of this snippet is:
<?xml version="1.0" encoding="utf-8" ?>
<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
<CodeSnippet Format="1.0.0">
<Header>
<Title>propnp</Title>
<Shortcut>propnp</Shortcut>
<Description>Code snippet for property with OnPropertyChanged</Description>
<Author>Bradley</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>
<Literal>
<ID>field</ID>
<ToolTip>The variable backing this property</ToolTip>
<Default>myVar</Default>
</Literal>
</Declarations>
<Code Language="csharp"><![CDATA[public $type$ $property$
{
get { return $field$;}
set
{
$field$ = value;
OnPropertyChanged($property$);
}
}
$end$]]>
</Code>
</Snippet>
</CodeSnippet>
</CodeSnippets>