Welcome to David Sandor Sign in | Join | Help
in Search

Professional Software Architecture

Code Snippet for Dependency Property (WPF/SL)

I am writing some User Controls for a WPF application and needed a quick way to create my Dependency Properties for my control.

Below is the code snippet.

First element is the type, next the Property Name, then the Containing Class name (you need this for the Dependency Property Registration call.

<?xml version="1.0" encoding="utf-8" ?>
<CodeSnippets  xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
    <CodeSnippet Format="1.0.0">
        <Header>
            <Title>Dependency Property</Title>
            <Shortcut>dp</Shortcut>
            <Description>Code snippet for dependency property</Description>
            <Author>David Sandor</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 (name of the CLR property).</ToolTip>
                    <Default>MyProperty</Default>
                </Literal>
                <Literal>
                    <ID>className</ID>
                    <ToolTip>Name of the class that contains this DP.</ToolTip>
                    <Default>MyClass</Default>
                </Literal>
            </Declarations>
            <Code Language="csharp"><![CDATA[
        public $type$ $property$
        {
            get { return ($type$)this.GetValue($property$Property); }
            set { this.SetValue($property$Property,value); }
        }
        public static readonly DependencyProperty $property$Property =
         DependencyProperty.Register(
            "$property$", typeof($type$), typeof($className$));
$end$]]>
            </Code>
        </Snippet>
    </CodeSnippet>
</CodeSnippets>

The snippet will generate out code like this:

public string AddressLine1
{
    get { return (string)this.GetValue(AddressLine1Property); }
    set { this.SetValue(AddressLine1Property, value); }
}
public static readonly DependencyProperty AddressLine1Property =
 DependencyProperty.Register(
    "AddressLine1", typeof(string), typeof(Address));

Here is the snippet file click here to download.

Published Sunday, July 05, 2009 10:59 PM by david

Comments

No Comments
Anonymous comments are disabled

This Blog

Syndication

Powered by Community Server (Personal Edition), by Telligent Systems