Over a million developers have joined DZone.

Dependency Properties and Silverlight 2

DZone's Guide to

Dependency Properties and Silverlight 2

· ·
Free Resource

One aspect of Custom Control development is the understanding of Dependency Properties. Essentially what dependency properties do, is enhance the properties of your custom controls by utilizing what Microsoft has called Silverlight 2's Property System. Theres quite a bit of information about dependency Properties and you can get the MSDN lowdown here: http://msdn.microsoft.com/en-us/library/cc221408(VS.95).aspx; however, I figured I would explain what I have learned about them and give you the quick tour.

First, why should you use Dependency Properties in your controls? What do you gain by using them? In short these are my main reasons I have come up with. 

  1. Data Binding Support. You can only data bind dependency properties in Silverlight 2.
  2. Animation Support. To animate using the property in Silverlight 2 requires the property to be a dependency property.
  3. XAML Styling support, theres alot of aspects of styling not supported unless the property is a Dependency Property. This includes mainly Template Binding and Default Value settings mainly, but I think it also includes VSM support but since VSM is based on the animation and tweening, that should be obvious from my second reason.
  4. Advanced Topic: Something I dont want to really cover right now or explain how to utilize but another advantage to using dependency properties is Attached Properties such as the use of "Grid.Column" and "Grid.Row" where the property is attached to a child element. Something that requires a little more of its own explanation of when, why, how to do such a thing.

Thats about it, not a huge list, but those are the main aspects of why you should use Dependency Properties.

The next part I would like to talk about how to use dependency properties, what does it take to build dependency properties. For the most part dependency properties are fairly straight forward but they are slightly more complex to create than traditional properties. I think the best way to show "how" to use dependency properties is to simply show an example of one.

old property

private decimal width = 20.0;

public decimal Width
get { return width; }
set { width = value; }

dependency property

public static readonly DependencyProperty WidthProperty =
typeof(NAME_OF_CONTROL), //example typeof( LoadingCurtain)
new PropertyMetadata(20.0,
new PropertyChangedCallback(OnWidthChanged)

public decimal Width
get { return (decimal)GetValue(WidthProperty); }
set { SetValue(WidthProperty, value); }

private static void OnWidthChanged(DependencyObject d,
DependencyPropertyChangedEventArgs e)
decimal _width = (decimal)d;

What is important to note here in this sample is there are 3 seperate things:

 1. There is a read only static declaration of the dependency property. The second "typeof(NAME_OF_CONTROL)" would actually be the type of the custom control you are housing the dependency property or a better way to put it is the custom control with the dependency property.

2. The second object looks very similar to any regular property except when setting the values it uses the GetValue and SetValue commands you probably have seen in various other controls in Silverlight 2. This is still required to make your property a real property in your control, its the wiring that is what is important, DO NOT put any other additional code in this properties get and set blocks.

3. The final bit is the on property changed event handler, this is where ALL logic goes after the property has been changed. Its important to do any and all logic here.

That concludes my primer on Dependency properties, I hope to cover more advanced topics on silverlight 2 custom controls as I dive into them.



Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}