Share via


DP Property Changed Snippet

<?xml version="1.0" encoding="utf-8" ?>
<CodeSnippets  xmlns="https://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
    <CodeSnippet Format="1.0.0">
        <Header>
            <Title>Define a DependencyProperty with PropertyChanged</Title>
            <Shortcut>propdp</Shortcut>
            <Description>Code snippet for a property using DependencyProperty as the backing store</Description>
            <Author>jfoscoding</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>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>
            </Declarations>
            <Code Language="csharp">
                <![CDATA[

public $type$ $property$ {
    get { return ($type$)GetValue($property$Property); }
    set { SetValue($property$Property, value); }
}

// Using a DependencyProperty as the backing store for $property$.  This enables animation, styling, binding, etc...
public static readonly DependencyProperty $property$Property =
    DependencyProperty.Register("$property$", typeof($type$), typeof($ownerclass$), new UIPropertyMetadata($defaultvalue$, On$property$Changed));

private static void On$property$Changed(DependencyObject dobj, DependencyPropertyChangedEventArgs e) {
   $ownerclass$ owner = dobj as $ownerclass$;
   if (owner != null) {
     // todo: execute property changed code.
   }
}
$end$]]>
            </Code>
        </Snippet>
    </CodeSnippet>
</CodeSnippets>

jfopropertychanged.snippet