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 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="">

  <CodeSnippet Format="1.0.0">





      <Title>Define a DependencyProperty</Title>



        Code snippet for a property using DependencyProperty as the backing

        store and a Handler for the DependencyPropertyChanged event


      <Author>Mike Brown</Author>




        <Literal Editable="true">


          <ToolTip>Property Type</ToolTip>





        <Literal Editable="true">


          <ToolTip>Property Name</ToolTip>





        <Literal Editable="false">



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

            declared in.






      <Code Language="csharp">



public $type$ $property$


    get { return ($type$)GetValue($property$Property); }

    set { SetValue($property$Property, value); }


public static readonly DependencyProperty $property$Property = DependencyProperty.Register(




  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.










  1. Very similar to something I posted a few months ago here:
    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.

Leave a Reply

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

You are commenting using your 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: