Skip navigation

So it finally got to the point where I was tired enough of "enhancing" my dependency properties by hand that I felt it was worth the investment to learn how to write code snippets. The "Orcas" preview for VS2005 already comes with a few snippets to simplify declaring dependency properties and attached dependency properties (propdp and propa, respectively). But it was missing a few things…I stumbled across some enhanced snippets at notstatic.com. But it was still missing something, I needed my snippet to automatically generate the PropertyChangedCallback. So I opened up the snippet to see what I needed to do to add that enhancement.
 
Surprisingly enough it was quite simple. The snippet uses token substitution to place named parameters into the code. I guess it’d be easier to show you. If anyone else needs this, just save it as propdh.snippet under your %vsinstall%\VC#\Snippets\%lcid%\Visual C# directory where %vsinstall% is your installation root of VS2005 and %lcid% is your locale identifier (1033 for us english).
 

<?xml version="1.0" encoding="utf-8"?>

<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">

  <CodeSnippet Format="1.0.0">

    <Header>

      <SnippetTypes>

        <SnippetType>Expansion</SnippetType>

      </SnippetTypes>

      <Title>Define a DependencyProperty</Title>

      <Shortcut>propdph</Shortcut>

      <Description>

        Code snippet for a property using DependencyProperty as the backing

        store and a Handler for the DependencyPropertyChanged event

      </Description>

      <Author>Mike Brown</Author>

    </Header>

    <Snippet>

      <Declarations>

        <Literal Editable="true">

          <ID>type</ID>

          <ToolTip>Property Type</ToolTip>

          <Default>int</Default>

          <Function>

          </Function>

        </Literal>

        <Literal Editable="true">

          <ID>property</ID>

          <ToolTip>Property Name</ToolTip>

          <Default>MyProperty</Default>

          <Function>

          </Function>

        </Literal>

        <Literal Editable="false">

          <ID>ownerclass</ID>

          <ToolTip>

            The owning class of this Property. Typically the class that it is

            declared in.

          </ToolTip>

          <Default>ClassNamePlaceholder</Default>

          <Function>ClassName()</Function>

        </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 UIPropertyMetadata(default($type$), new PropertyChangedCallback(On$property$Changed)));

 

private static void On$property$Changed(DependencyObject sender, DependencyPropertyChangedEventArgs args)

{

  $ownerclass$ tmp$ownerclass$ = sender as $ownerclass$;

  if (tmp$ownerclass$!=null)

  {

    //TODO: Do what you need to do with the new value.

  }

}

$end$]]>

      </Code>

    </Snippet>

  </CodeSnippet>

</CodeSnippets>

 
Advertisements

3 Comments

  1. Very similar to something I posted a few months ago here: http://www.wiredprairie.us/journal/2007/02/a_few_handy_wpf_visual_studio.html
    I\’ve found having the FrameworkPropertyMetadata class to be more useful than the UIPropertyMetadata on average.

  2. I wasn\’t aware of your code snippet, but I agree they are very similar. Can you explain what the advantage of FPM is over UIPM? I seem to recall having a callback that was generic, but I can\’t find hide nor hair of it now.

  3. It\’s the FrameworkPropertyMetadataOptions that I always go for — little options that can automatically affect the behavior. I always use FrameworkPropertyMetaData over the other options. Everything else is very close.
    FrameworkPropertyMetadataOptions.AffectsRender


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: