Last active
December 6, 2017 21:57
-
-
Save aaronhoffman/c821acf4bb3b3beb0759e93bbe3a2402 to your computer and use it in GitHub Desktop.
Visual Studio 2017 Code Snippet INotifyPropertyChanged
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static void OnPropertyChanged<T>( | |
this INotifyPropertyChanged @this, | |
PropertyChangedEventHandler propertyChanged, | |
Expression<Func<T>> propertyExpr) | |
{ | |
if (propertyChanged == null) return; | |
var memberExpr = (MemberExpression)propertyExpr.Body; | |
var ea = new PropertyChangedEventArgs(memberExpr.Member.Name); | |
propertyChanged(@this, ea); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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; | |
this.OnPropertyChanged(PropertyChanged, () => $property$); | |
} | |
} | |
} | |
private $type$ _$property$;$end$]]> | |
</Code> | |
</Snippet> | |
</CodeSnippet> | |
</CodeSnippets> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment