-- online Search!

Dependency Property Generator

by kirupa   | filed under Silverlight, WPF, and Blend

Modern version

This tutorial uses Silverlight, which browsers no longer run. The same idea with today's HTML, CSS, and JavaScript: Getters and Setters.

TL;DR

Kirupa's Dependency Property Generator writes dependency property code for you, since the syntax is hard to remember. Enter the Property Name, the Property Type, and the Owning Class Name, hit Generate Code, and paste the output inside your owning class. You get the CLR wrapper, the registered identifier, and an empty property changed callback.

  • The Property Name is what you'll see in the Properties Inspector of Blend or Visual Studio, and the Property Type could be string, double, or a custom type.
  • The most common mistake is pasting the code outside your owning class's boundaries, which gives you a build error or a property that doesn't work.
  • Default values are only filled in for string, double, and int. For other types, add your default as the first PropertyMetadata argument.

This is an archived tutorial from the kirupa.com legacy collection. It covers software that may no longer be available, but it is kept online because the ideas still hold up.

This site has a fair number of articles on dependency properties, but at the end of the day, they are still really hard to write. Remembering the syntax and making sure the C# equivalent of the i's and t's are dotted and crossed is very hard - even for experienced users.

To make this tedious task easier, I've written a small app that will create Dependency Properties for you as long as you give it the type, owning class, and property name. The app can be found below:

If you are a reader of my blog, the above application should be old news to you. What will be new is the information on how to actually use the app and integrate its output into your own project.

Using the Dependency Property Generator

This little app asks for three pieces of information. First, you will be asked to enter your Property Name. This is the name you want to give your dependency property so that you can use it later on. To look at it another way, this is the value that you will see in the Properties Inspector of either Expression Blend or Visual Studio.

The next piece of information you will be asked is the Property Type. Your property type represents what form the data you store in the dependency property will be taking. Example values include string, double, int, MyCustomType, List, etc.

Finally, you will be asked the Owning Class Name. Your dependency property will be living inside something. This something is a class that you paste your dependency property code into. For example, let's look the code for a simple user control that I just created in Expression Blend:

using System;
using System.Collections.Generic;
using System.Net;
using System.Windows;
using System.Windows.Controls;
namespace DeepZoomProject
{
  public partial class CustomPage : UserControl
  {
  public CustomPage()
  {
  InitializeComponent();
  }
  }
}

This user control's name is CustomPage, and my dependency property will live inside this usercontrol. This name is also the class name, so therefore, my Owning Class Name in this case will be CustomPage.

Integrating your Dependency Property

To close this example out, let's say I want to create a dependency property. My property will be called CustomName, its type will be string, and its owning class is CustomPage. I hit the Generate Code button, and out pops the following:

public string CustomName
{
  get { return (string)GetValue(CustomNameProperty); }
  set { SetValue(CustomNameProperty, value); }
}
public static readonly DependencyProperty CustomNameProperty = DependencyProperty.Register("CustomName", typeof(string), typeof(CustomPage), new PropertyMetadata("",CustomNameChanged));
private static void CustomNameChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
  // Code for dealing with your property changes
}

Pasting the above into my CustomPage class, I get the following:

using System;
using System.Collections.Generic;
using System.Net;
using System.Windows;
using System.Windows.Controls;
namespace DeepZoomProject
{
  public partial class CustomPage : UserControl
  {
  public string CustomName
  {
  get { return (string)GetValue(CustomNameProperty); }
  set { SetValue(CustomNameProperty, value); }
  }
  public static readonly DependencyProperty CustomNameProperty = DependencyProperty.Register("CustomName", typeof(string), typeof(CustomPage), new PropertyMetadata("",CustomNameChanged));
  private static void CustomNameChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
  {
  // Code for dealing with your property changes
  }
  public CustomPage()
  {
  InitializeComponent();
  }
  }
}

Notice that I pasted my dependency property code inside the area defined by my CustomPage class. The most common mistake is that you paste it outside your owning class's boundaries. Don't do that unless you want to see a build error or your dependency property not working.

That is all there is to filling in the three values, getting the code for your dependency property, and then simply pasting the code into the class you want the property to be exposed on.

Setting Default Values

The final detail is the default value that your dependency property may require. Currently, outside of string, double, and int types, I do not specify a default value for your dependency property. The reason is that each type will have its own unique way of specifying a default value for itself.

The default value is specified in the line defining your dependency property identifier:

public static readonly DependencyProperty CustomNameProperty = DependencyProperty.Register("CustomName", typeof(string), typeof(CustomPage), new PropertyMetadata("",CustomNameChanged));

More specifically, look in your PropertyMetadata constructor: new PropertyMetadata("",CustomNameChanged));

If there are two arguments, the first argument specifies the default value. For property types other than string, int, and double, you will not see two arguments. The only argument you see will be for the property changed callback, so insert your default value as the first argument to your PropertyMetadata constructor.


Before you go, here are a few things you can do:

  1. Ask a question

    Stuck on something, or curious about a detail we skipped? Ask on the forums, and we’ll help you out.

Just a final word before we wrap up. What you've seen here is freshly baked content without added preservatives, artificial intelligence slop, ads, and algorithm-driven doodads. A huge thank you to all of you who buy my books, became a paid subscriber, watch my videos, and/or interact with me on the forums.

Your support keeps this site going! 😇

Kirupa's signature!

The KIRUPA Newsletter

Thought provoking content that lives at the intersection of design 🎨, development 🤖, and business 💰 - delivered weekly to over a bazillion subscribers!

SUBSCRIBE NOW

Creating engaging and entertaining content for designers and developers since 1998.

Follow:

Popular

Loose Ends

:: Copyright © KIRUPA 2026 //--