Jan 18

Getting started with dependency properties

Posted in Silverlight      Comments Off on Getting started with dependency properties

The dependency property concept was introduced by WPF 1.0 for exposing rich functionality from XAML. Dependency properties support several features exposed by Silverlight (styling, animation and data binding, for instance). Unfortunately, they also increase the complexity associated with property definition. Dependency properties values at runtime depend on several things. For instance, it could depend on an animation which continuously changes the value of a property or it could depend on the value of its parent property (ie, it could inherit the value from its parent if the value isn’t explicitly set). When compared with the basic CLR properties, dependency property introduces the following advantages:

  • change notifications;
  • property value inheritance;
  • support for multiple “providers” which can set its value.

Now that I’ve presented some of the ideas behind the concept, lets see how dependency properties are implemented. Dependency properties are exposed through “normal” CLR properties which wrap access to a field created through the DependencyProperty.register method. Take a look at the following snippet (from the ButtonBase class):


public abstract class ButtonBase : ContentControl {
    public static readonly DependencyProperty ClickModeProperty;
    static ButtonBase() {
        ClickModeProperty = DependencyProperty.Register(
        new PropertyMetadata(
new PropertyChangedCallback(
ButtonBase.OnClickModePropertyChanged))); //removed } public ClickMode ClickMode { get { return (ClickMode)base.GetValue(ClickModeProperty); } set { base.SetValue(ClickModeProperty, (Enum)value); } } private static void OnClickModePropertyChanged(
DependencyObject d,
DependencyPropertyChangedEventArgs e) { ClickMode newValue = (ClickMode)e.NewValue; if (((newValue != ClickMode.Release) &&
(newValue != ClickMode.Press)) &&
(newValue != ClickMode.Hover)) { throw new ArgumentException(…); } } }

ClickMode is a dependency property. The ClickModeProperty is the real dependency property which is wrapped through the ClickMode property. By convention,all dependency properties are stored through public static fields and their names ends with the Property suffix. Another conclusion which can be taken from the previous snippet is that dependency properties are always created through the DependencyProperty.Register method. This method expects the following parameters:

  • name: sets the name of the dependency property. This name is the same as the one that is used for the CLR property which wraps the dependency property field;
  • type: identifies the type of the dependency property that is registered;
  • owner: identifies the type of the object that “owns” the current dependency property;
  • metadata: object of type PropertyMetadata which lets you set the default value and/or a notification callback method.

After introducing the dependency property field and creating the dependency property through the register method, convention (and easy of use) makes us add a new property which wraps the dependency property field. As you can see from the previous snippet, the ClickMode property is a read/write property which uses the GetValue/SetValue methods inherited from the DependencyObject base class.

Do notice that unlike WPF, where everything is managed, in Silverlight dependency properties values depend on unmanaged code. Besides that, you should also keep in mind that Silverlight’s dependency properties don’t expose the same amount of functionality you have in WPF. And I guess this is more than enough for getting started with dependency properties. We’ll keep looking at them in the next posts. Stay tuned!