Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

How to Implement a DependencyProperty?

0.00/5 (No votes)
8 Sep 2009 1  
A DependencyProperty is set to enable declarative code to alter the properties of an object which reduces the data requirements by providing a more powerful notification system regarding the change of data in a very specific way.

A DependencyProperty is set to enable declarative code to alter the properties of an object which reduces the data requirements by providing a more powerful notification system regarding the change of data in a very specific way. In .NET, there are two types of properties: the normal property, and a Dependency Property which adds functionality over the normal property.

Now, let us discuss on how to implement such a Dependency Property to give a powerful notification on a data change.

First of all, implement the UserControl class from the INotifyPropertyChanged interface:

C#
public partial class MyUserControl : UserControl, INotifyPropertyChanged 
{ 
    public event PropertyChangedEventHandler PropertyChanged; 
    protected void OnPropertyChanged(string propertyName) 
    { 
        if (PropertyChanged != null) 
        { 
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); 
        } 
    } 
}

Create your own normal property. Let's say the name of the property is “Caption”.

C#
public string Caption 
{  
    get { return GetValue(CaptionProperty).ToString(); }  
    set { SetValue(CaptionProperty, value); } 
}

Now, register the DependencyProperty to the CLR by calling the Register method and by passing the property field that you used to store the data in the earlier step:

C#
public static readonly DependencyProperty CaptionProperty = 
  DependencyProperty.Register("Caption", typeof(string), typeof(MyUserControl), 
  new PropertyMetadata(string.Empty, OnCaptionPropertyChanged));

The name of the identifier field of the DependencyProperty will be same as you used in the property after appending “Property” at the end. In this example, our property name is “Caption”, hence our identifier field name is “CaptionProperty”. Add the PropertyMetaData with the default value and the callback event handler within the Register method as mentioned in the above code. Mark the identifier as static and readonly so that this will be unique to the CLR.

Now, implement the OnCaptionPropertyChanged event handler:

C#
private static void OnCaptionPropertyChanged(DependencyObject dependencyObject, 
               DependencyPropertyChangedEventArgs e) 
{  
    MyUserControl myUserControl = dependencyObject as MyUserControl;  
    myUserControl.OnPropertyChanged("Caption");  
    myUserControl.OnCaptionPropertyChanged(e); 
}
private void OnCaptionPropertyChanged(DependencyPropertyChangedEventArgs e) 
{ 
    txbCaption.Text = Caption; 
}

The implementation of the Dependency Property is complete. You can either call it from XAML:

XML
<local:MyUserControl Caption="My First Dependency Property Example" />

or from the code-behind:

C#
MyUserControl myUserControl = new MyUserControl(); 
myUserControl.SetValue(MyUserControl.CaptionProperty, 
              "My First Dependency Property Example");

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here