Created
August 6, 2013 20:39
-
-
Save philchuang/6168400 to your computer and use it in GitHub Desktop.
Visual Studio snippets for * quickly creating a MVVM property that raises INotifyPropertyChanged.PropertyChanged
* quickly creating a MVVM DependencyProperty And related static methods for determining a property name via reflection at runtime
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 mlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet"> | |
<CodeSnippet Format="1.0.0"> | |
<Header> | |
<SnippetTypes> | |
<SnippetType>Expansion</SnippetType> | |
</SnippetTypes> | |
<Title>MVVM Dependency Property</Title> | |
<Author>Phil Chuang</Author> | |
<Description>A snippet for generating a Dependency Property</Description> | |
<HelpUrl></HelpUrl> | |
<Shortcut>mvvmdepprop</Shortcut> | |
</Header> | |
<Snippet> | |
<Declarations> | |
<Literal Editable="true"> | |
<ID>PropertyType</ID> | |
<ToolTip>Property type</ToolTip> | |
<Default>Object</Default> | |
<Function> | |
</Function> | |
</Literal> | |
<Literal Editable="true"> | |
<ID>PropertyName</ID> | |
<ToolTip>Property name</ToolTip> | |
<Default>Property</Default> | |
<Function> | |
</Function> | |
</Literal> | |
<Literal default="true" Editable="false"> | |
<ID>ClassName</ID> | |
<ToolTip>Class name</ToolTip> | |
<Function>ClassName()</Function> | |
<Default>ClassNamePlaceholder</Default> | |
</Literal> | |
</Declarations> | |
<Code Language="CSharp"> | |
<![CDATA[public $PropertyType$ $PropertyName$ | |
{ | |
get { return ($PropertyType$) GetValue ($PropertyName$Property); } | |
set { SetValue ($PropertyName$Property, value); } | |
} | |
public static readonly DependencyProperty $PropertyName$Property = DependencyProperty.Register ( | |
INotifyPropertyChangedHelper.GetPropertyName (($ClassName$ c) => c.$PropertyName$), | |
typeof ($PropertyType$), | |
typeof ($ClassName$), | |
new PropertyMetadata (null, On$PropertyName$Changed)); | |
private static void On$PropertyName$Changed (DependencyObject d, DependencyPropertyChangedEventArgs e) | |
{ | |
var instance = d as $ClassName$; | |
if (instance == null) return; | |
var oldValue = ($PropertyType$) e.OldValue; | |
var newValue = ($PropertyType$) e.NewValue; | |
// TODO implement | |
}]]> | |
</Code> | |
</Snippet> | |
</CodeSnippet> | |
</CodeSnippets> |
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 class INotifyPropertyChangedHelper | |
{ | |
public static String GetPropertyName<T> (Expression<Func<T>> propertyExpression) | |
{ | |
if (propertyExpression == null) throw new ArgumentNullException ("propertyExpression"); | |
if (propertyExpression.Body.NodeType == ExpressionType.MemberAccess) | |
{ | |
var memberExpr = (MemberExpression) propertyExpression.Body; | |
return memberExpr.Member.Name; | |
} | |
if (propertyExpression.Body.NodeType == ExpressionType.Convert | |
&& propertyExpression.Body is UnaryExpression | |
&& ((UnaryExpression) propertyExpression.Body).Operand.NodeType == ExpressionType.MemberAccess) | |
{ | |
var memberExpr = (MemberExpression) ((UnaryExpression) propertyExpression.Body).Operand; | |
return memberExpr.Member.Name; | |
} | |
throw new Exception (String.Format ("Expected MemberAccess expression, got {0}", propertyExpression.Body.NodeType)); | |
} | |
public static String GetPropertyName<T, V> (Expression<Func<T, V>> propertyExpression) | |
{ | |
if (propertyExpression == null) throw new ArgumentNullException ("propertyExpression"); | |
if (propertyExpression.Body.NodeType == ExpressionType.MemberAccess) | |
{ | |
var memberExpr = (MemberExpression) propertyExpression.Body; | |
return memberExpr.Member.Name; | |
} | |
if (propertyExpression.Body.NodeType == ExpressionType.Convert | |
&& propertyExpression.Body is UnaryExpression | |
&& ((UnaryExpression) propertyExpression.Body).Operand.NodeType == ExpressionType.MemberAccess) | |
{ | |
var memberExpr = (MemberExpression) ((UnaryExpression) propertyExpression.Body).Operand; | |
return memberExpr.Member.Name; | |
} | |
throw new Exception (String.Format ("Expected MemberAccess expression, got {0}", propertyExpression.Body.NodeType)); | |
} | |
} |
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 mlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet"> | |
<CodeSnippet Format="1.0.0"> | |
<Header> | |
<SnippetTypes> | |
<SnippetType>Expansion</SnippetType> | |
</SnippetTypes> | |
<Title>MVVM Property</Title> | |
<Author>Phil Chuang</Author> | |
<Description>A snippet for generating a backed Property that calls RaisePropertyChanged</Description> | |
<HelpUrl></HelpUrl> | |
<Shortcut>mvvmprop</Shortcut> | |
</Header> | |
<Snippet> | |
<Declarations> | |
<Literal Editable="true"> | |
<ID>Type</ID> | |
<ToolTip>Property type</ToolTip> | |
<Default>Object</Default> | |
<Function> | |
</Function> | |
</Literal> | |
<Literal Editable="true"> | |
<ID>PropertyName</ID> | |
<ToolTip>Property name</ToolTip> | |
<Default>Property</Default> | |
<Function> | |
</Function> | |
</Literal> | |
</Declarations> | |
<Code Language="CSharp"> | |
<![CDATA[private $Type$ my$PropertyName$; | |
public $Type$ $PropertyName$ | |
{ | |
get { return my$PropertyName$; } | |
set | |
{ | |
my$PropertyName$ = value; | |
RaisePropertyChanged (() => $PropertyName$); | |
} | |
}]]> | |
</Code> | |
</Snippet> | |
</CodeSnippet> | |
</CodeSnippets> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment