Click here to Skip to main content
13,771,871 members
Click here to Skip to main content
Add your own
alternative version

Tagged as


30 bookmarked
Posted 8 Sep 2009
Licenced CPOL

How to Implement a DependencyProperty?

Rate this:
Please Sign up or sign in to vote.
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:

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

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:

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:

private static void OnCaptionPropertyChanged(DependencyObject dependencyObject, 
               DependencyPropertyChangedEventArgs e) 
    MyUserControl myUserControl = dependencyObject as MyUserControl;  
private void OnCaptionPropertyChanged(DependencyPropertyChangedEventArgs e) 
    txbCaption.Text = Caption; 

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

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

or from the code-behind:

MyUserControl myUserControl = new MyUserControl(); 
              "My First Dependency Property Example");


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Kunal Chowdhury «IN»
Software Developer (Senior)
India India
Kunal Chowdhury is a Microsoft "Windows Platform Development" MVP (Most Valuable Professional), a Codeproject Mentor, Telerik Developer Expert, Nokia Developer Champion, Windows 10 Champion, Microsoft Rockstar, Speaker in various Microsoft events, Author, passionate Blogger and a Software Engineer by profession.

He is currently working in an MNC located in India. He has a very good skill over XAML, C#, Silverlight, Windows Phone, WPF and Windows Store (WinRT) app development. He posts his findings, articles, tutorials in his technical blog and CodeProject.

Book authored:

Connect with me on:

My CodeProject Awards/Prizes:

You may also be interested in...

Comments and Discussions

QuestionMy vote of 5 Pin
Tim Butler18-Apr-17 13:54
memberTim Butler18-Apr-17 13:54 
QuestionCode behind doesn't work Pin
Member 1063968028-Apr-16 12:09
memberMember 1063968028-Apr-16 12:09 
QuestionSmall clarification on code. Pin
Member 956059124-Oct-14 20:29
memberMember 956059124-Oct-14 20:29 
AnswerRe: Small clarification on code. Pin
rahul_tripathikanpur2-Oct-16 22:45
memberrahul_tripathikanpur2-Oct-16 22:45 
GeneralMy vote of 1 Pin
alamgir15-May-14 19:35
memberalamgir15-May-14 19:35 
GeneralMy vote of 3 Pin
StewBob18-Aug-13 5:46
memberStewBob18-Aug-13 5:46 
GeneralMy vote of 5 Pin
mhdnour14-Apr-12 9:00
membermhdnour14-Apr-12 9:00 
GeneralMy vote of 5 Pin
Member 476816328-Feb-12 1:40
memberMember 476816328-Feb-12 1:40 
GeneralMy vote of 4 Pin
bluetiger092013-Apr-11 18:57
memberbluetiger092013-Apr-11 18:57 
GeneralMy vote of 2 Pin
Patel Kalpesh15-Feb-11 20:54
memberPatel Kalpesh15-Feb-11 20:54 
GeneralMy vote of 1 Pin
mahendra varman29-Dec-10 20:00
membermahendra varman29-Dec-10 20:00 
QuestionWhat the Identifier should be defined readonly ??? Pin
Member 330120119-Nov-10 1:33
memberMember 330120119-Nov-10 1:33 
GeneralMy vote of 1 Pin
phobik26-Oct-10 15:15
memberphobik26-Oct-10 15:15 
GeneralRe: My vote of 1 Pin
Kunal Chowdhury «IN»7-Nov-10 22:50
mentorKunal Chowdhury «IN»7-Nov-10 22:50 
Generalone small correction Pin
Shanbhag uday20-Mar-10 4:52
memberShanbhag uday20-Mar-10 4:52 
AnswerRe: one small correction Pin
Kunal Chowdhury «IN»10-Jul-10 3:09
mentorKunal Chowdhury «IN»10-Jul-10 3:09 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web03 | 2.8.181119.1 | Last Updated 8 Sep 2009
Article Copyright 2009 by Kunal Chowdhury «IN»
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid