Here are a handful of simple snippets I've made to some existing WPF snippets that I find useful when coding .NET 3.0.
Paste them into a file with the extension
.snippet
and place them in the following directory:
…your account…\Documents\Visual Studio 2005\Code Snippets\Visual C#\My Code Snippets
Â
Restart Visual Studio 2005 and the new snippets should be available.
The snippets included are:
Name | Description |
propdpc
| Create a new dependency property, using FrameworkPropertyMetadata, which is normally more useful than PropertyMetadata, and also an event to handle when the property changes (for those occasions when the Options flags don't suffice). |
propdpf
| Same as above, although without the event. |
Rpnc
| Code for "RaisePropertyNotifyChanged". If you implement INotifyPropertyChanged and like to encapsulate the call to the event, this is the function you'll often want. |
Propr
| A standard .NET backed property, but calls the function defined in the previous snippet. |
Â
<?xml
version="1.0"
encoding="utf-8" ?>
<CodeSnippets
xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
    <CodeSnippet
Format="1.0.0">
        <Header>
            <Title>Define a DependencyProperty</Title>
            <Shortcut>propdpc</Shortcut>
            <Description>Dependency property, using frameworkPropertyMetadata, with PropertyChanged event</Description>
            <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>ownerclass</ID>
                    <ToolTip>The owning class of this Property. Typically the class that it is declared in.</ToolTip>
                    <Default>ownerclass</Default>
                </Literal>
                <Literal>
                    <ID>defaultvalue</ID>
                    <ToolTip>The default value for this property.</ToolTip>
                    <Default>0</Default>
                </Literal>
                <Literal>
                    <ID>frameworkoptions</ID>
                    <ToolTip>The options for this property and how it affects the DP</ToolTip>
                    <Default>.None</Default>
                </Literal>
            </Declarations>
            <Code
Language="csharp">
                <![CDATA[public $type$ $property$
{
get { return ($type$)GetValue($property$Property); }
set { SetValue($property$Property, value); }
}
Â
public static readonly DependencyProperty $property$Property =
DependencyProperty.Register("$property$", typeof($type$), typeof($ownerclass$),
new FrameworkPropertyMetadata($defaultvalue$,
FrameworkPropertyMetadataOptions$frameworkoptions$,
new PropertyChangedCallback($property$PropertyChanged)));
Â
private static void $property$PropertyChanged(DependencyObject depObj, DependencyPropertyChangedEventArgs e)
{
// put code here to handle the property changed for $property$
$ownerclass$ $ownerclass$Obj = depObj as $ownerclass$;
if ($ownerclass$Obj != null)
{
Â
}
}
Â
$end$]]>
            </Code>
        </Snippet>
    </CodeSnippet>
    <CodeSnippet
Format="1.0.0">
        <Header>
            <Title>Define a DependencyProperty</Title>
            <Shortcut>propdpf</Shortcut>
            <Description>DependencyProperty, using FrameworkPropertyMetadata, no change notification</Description>
            <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>NameTheProperty</Default>
                </Literal>
                <Literal>
                    <ID>ownerclass</ID>
                    <ToolTip>The owning class of this Property. Typically the class that it is declared in.</ToolTip>
                    <Default>OwnerClassType</Default>
                </Literal>
                <Literal>
                    <ID>defaultvalue</ID>
                    <ToolTip>The default value for this property.</ToolTip>
                    <Default>0</Default>
                </Literal>
                <Literal>
                    <ID>frameworkoptions</ID>
                    <ToolTip>The options for this property and how it affects the DP</ToolTip>
                    <Default>.None</Default>
                </Literal>
            </Declarations>
            <Code
Language="csharp">
                <![CDATA[public $type$ $property$
{
get { return ($type$)GetValue($property$Property); }
set { SetValue($property$Property, value); }
}
Â
public static readonly DependencyProperty $property$Property =
DependencyProperty.Register("$property$", typeof($type$), typeof($ownerclass$),
new FrameworkPropertyMetadata($defaultvalue$,
FrameworkPropertyMetadataOptions$frameworkoptions$));
Â
$end$]]>
            </Code>
        </Snippet>
    </CodeSnippet>
    <CodeSnippet
Format="1.0.0">
        <Header>
            <Title>rpnc</Title>
            <Shortcut>rpnc</Shortcut>
            <Description>Code snippet for a RaisePropertyChanged method (used by propr)</Description>
            <Author></Author>
        </Header>
        <Snippet>
            <Code
Language="csharp">
                <![CDATA[protected void RaisePropertyChanged(string propertyName)
{
System.Diagnostics.Debug.Assert(!string.IsNullOrEmpty(propertyName));
Â
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
$end$]]>
            </Code>
        </Snippet>
    </CodeSnippet>
    <CodeSnippet
Format="1.0.0">
        <Header>
            <Title>propr</Title>
            <Shortcut>propr</Shortcut>
            <Description>Code snippet for property and backing field with Notification via RaisePropertyChanged method</Description>
            <Author></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[private $type$ $field$;
Â
public $type$ $property$
{
get { return $field$;}
set
{
if ($field$ != value)
{
$field$ = value;
RaisePropertyChanged("$property$");
}
}
}
$end$]]>
            </Code>
        </Snippet>
    </CodeSnippet>
</CodeSnippets>
Â
Â