Silverlight Dependency Properties

摘要:Silverlight Dependency Properties

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.

WHY
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.
 

HOW
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

   1:  private decimal width = 20.0;
   2:  public decimal Width
   3:  {
   4:     get { return width; }
   5:     set { width = value; }
   6:  } 


dependency property

   1:  public static readonly DependencyProperty WidthProperty =
   2:    DependencyProperty.Register(
   3:    "Width",
   4:    typeof(decimal),
   5:    typeof(NAME_OF_CONTROL), //example typeof( LoadingCurtain)
   6:    new PropertyMetadata(20.0,
   7:        new PropertyChangedCallback(OnWidthChanged)
   8:    )
   9:  );
  10:  public decimal  Width
  11:  {
  12:      get { return (decimal)GetValue(WidthProperty); }
  13:      set { SetValue(WidthProperty, value); }
  14:  }
  15:  private static void OnWidthChanged(DependencyObject d,
  16:      DependencyPropertyChangedEventArgs e)
  17:  {
  18:      decimal _width = (decimal)d;
  19:      ...
  20:  }


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.