Visual Studio 2010 DependencyProperty Code snippets
I had to change a couple of properties to Dependency Properties in Silverlight/C# today. Driving home I thought it would be better to have code snippets so next time it would be easier.
I made two snippets:
propdepreg generated code:
public int MyProperty
{
get { return (int)GetValue(MyPropertyProperty); }
set { SetValue(MyPropertyProperty, value); }
}
public static DependencyProperty MyPropertyProperty = DependencyProperty.Register("MyProperty", typeof(int), typeof(dataviewcontrol), null);
The other snippet includes a PropertyChangedCallback method.
propdepregchanged generated code:
public int MyProperty
{
get { return (int)GetValue(MyPropertyProperty); }
set { SetValue(MyPropertyProperty, value); }
}
public static DependencyProperty MyPropertyProperty = DependencyProperty.Register("MyProperty", typeof(int), typeof(ClassNamePlaceholder), new PropertyMetadata(MyPropertyChanged));
private static void MyPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
// insert your code here
}
Download the two files below. To install open the Code Snippets Manager (Ctrl-K, Ctrl-B) of Visual Studio, choose C# and your favorite folder and click "Import" to import both files.
In your C# file you use it as normal snippets by typing
propdepreg + tab + tab
Now you can tab through the three fields:
- PropertyType
- PropertyName
- OwnerClass
Constructing a snippet is pretty easy.
A snippet consists of three parts:
- Header - Contains meta data like name of snippet, name, author, etc.
- Declarations - What 'variables' are to be filled in.
- Code - The generated code.
The fileformat is XML, defined like this:
<?xmlversion="1.0"encoding="utf-8" ?>
<CodeSnippetsxmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
<CodeSnippetFormat="1.0.0">
<Header>
</Header>
<Snippet>
<Declarations>
</Declarations>
<Code>
</Code>
</Snippet>
</CodeSnippet>
</CodeSnippets>
The header describes the snippet.
<Header>
<Title>DependencyProperty</Title>
<Shortcut>propdepreg</Shortcut>
<Description>
Code snippet for a dependency property including registering.
</Description>
<Author>Ing. J. Zandbergen, www.midity.com</Author>
<SnippetTypes>
<SnippetType>Expansion</SnippetType>
</SnippetTypes>
</Header>
There are 3 snippetTypes: Expansion, SurroundsWith and Refactoring. With expansion you can place text before and after a selection, like #region ..... #endregion for example. Refactoring is used for .... refactoring.
Declarations defines the fields the user has to fill:
<Declarations>
<LiteralEditable="true">
<ID>type</ID>
<ToolTip>Property type</ToolTip>
<Default>int</Default>
</Literal>
<LiteralEditable="true">
<ID>PropertyName</ID>
<ToolTip>Property name</ToolTip>
<Default>MyProperty</Default>
</Literal>
<LiteralEditable="true">
<ID>Owner</ID>
<ToolTip>Class name</ToolTip>
<Function>ClassName()</Function>
<Default>ClassNamePlaceholder</Default>
</Literal>
</Declarations>
Note the <Function>ClassName()</Function> which generates the classname of the surrounding class where you invoke the snippet.
Finally, the Code:
<CodeLanguage="CSharp">
<![CDATA[
public $type$ $PropertyName$
{
get { return ($type$)GetValue($PropertyName$Property); }
set { SetValue($PropertyName$Property, value); }
}
public static DependencyProperty $PropertyName$Property = DependencyProperty.Register("$PropertyName$", typeof($type$), typeof($Owner$), null);
]]>
</Code>
Every Literal is placed by using a $LiteralName$ construction.
I found it's easy to make snippets.
Check the directory C:\Program Files\Microsoft Visual Studio 10.0\VC#\Snippets for more.
You can open the snippet files from within VS 2010. Save them to %HOME%\Documents\Visual Studio 2010\Code Snippets\Visual C#\My Code Snippets\ (or somewhere around there) and they will be available in VS immediately.
VS has code completion for them, so editing is very very simple.
| Attachment | Size |
|---|---|
| propdepreg.snippet | 1.58 KB |
| propdepregchanged.snippet | 1.85 KB |
