When working with WPF you all may have come across the term Dependency Property, wondering what this may be and is it ever related to the property provided by CLR? So here I am explaining that with this article. Let's try to understand this amazing feature of WPF. Before doing that, I hope you are familiar with some basic knowledge of the CLR, Object-Oriented Programming and have made at least one simple WPF application.
Table of Contents
- WPF DEPENDENCY PROPERTY
- DEPENDENCY PROPERTY; WHAT IS IT?
- CLR PROPERTY VS. DEPENDENCY PROPERTY
- ADVANTAGES OF DEPENDENCY PROPERTY
- EXAMPLE
- CONCLUSION
Dependency Property; what is it?
WPF has provided some extended services to the CLR property that we can collectively call Dependency Properties. A Dependency Property is a property whose value depends on the external sources, such as animation, data binding, styles, or visual tree inheritance. Not only this, but a Dependency Property also has the built-in feature of providing notification when the property has changed, data binding and styling.
CLR Property vs. Dependency Property
A CLR property reads directly from the private member of the class. The Get() and Set() methods of the class retrieve and store the values of the property.
Advantages of a Dependency Property
- Less memory consumption
The Dependency Property stores the property only when it is altered or modified. Hence a huge amount of memory for fields are free.
- Property value inheritance
It means that if no value is set for the property then it will return to the inheritance tree up to where it gets the value.
- Change notification and Data Bindings
Whenever a property changes its value it provides notification in the Dependency Property using INotifyPropertyChange and also helps in data binding.
- Participation in animation, styles and templates
A Dependency Property can animate, set styles using style setters and even provide templates for the control.
- CallBacks
Whenever a property is changed you can have a callback invoked.
- Resources
You can define a Resource for the definition of a Dependency Property in XAML.
- Overriding Metadata
You can define certain behaviours of a Dependency Property using PropertyMetaData. Thus, overriding a metadata from a derived property will not require you to redefine or re-implement the entire property definition.
Example
To work with a Dependency Property, you must derive the class from a DependencyObject as the entire observer that holds the new Property System is defined within the DependencyObject. We will make a basic car selection application.
XAML
- <Window x:Class="WpfApplication1.DependencyPropertyDemo" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:WpfApplication1" Title="DependencyPropertyDemo" Height="350" Width="525">
- <Window.Resources>
- <ResourceDictionary>
- <local:CarDependencyProperty x:Key="CarDependency"></local:CarDependencyProperty>
- </ResourceDictionary>
- </Window.Resources>
- <Grid>
- <Grid.RowDefinitions>
- <RowDefinition />
- <RowDefinition />
- </Grid.RowDefinitions>
- <Label Content="Enter Car:" Grid.Row="0" VerticalAlignment="Center" />
- <TextBox Text="{Binding Path=MyCar, Source={StaticResource CarDependency }}" Name="MyTextCar" Height="25" Width="150" />
- <Button Name="MyButton" Content="Click Me!" Height="25" Click="MyButton_Click" Width="150" Grid.Row="1" />
- </Grid>
- </Window>
Code Behind
- namespace WpfApplication1 {
- /// <summary>
- /// Interaction logic for DependencyPropertyDemo.xaml
- /// </summary>
- public partial class DependencyPropertyDemo: Window {
- public DependencyPropertyDemo() {
- InitializeComponent();
- }
- private void MyButton_Click(object sender, RoutedEventArgs e) {
- CarDependencyProperty dpSample = TryFindResource("CarDependency") as CarDependencyProperty;
- MessageBox.Show(dpSample.MyCar);
- }
- }
- public class CarDependencyProperty: DependencyObject {
- //Register Dependency Property
- public static readonly DependencyProperty CarDependency = DependencyProperty.Register("MyProperty", typeof(string), typeof(CarDependencyProperty));
- public string MyCar {
- get {
- return (string) GetValue(CarDependency);
- }
- set {
- SetValue(CarDependency, value);
- }
- }
- }
- }
Conclusion
Thus, we have seen how a Dependency Property can bring wonders to our WPF application. This article is just intended to explain the basics of a Dependency Property. In the future we will see a deeper explanation regarding Dependency Properties. Hope you like it.

Sergei KulaginPosted May 6, 2020, 6:30 AM
That's like third article I've read and I still don't understand what dependency property is. So it's an object in your class. Your class should inherit from DependencyObject in order to use DependencyProperty. But I still don't understand what DependencyProperty is and how to use it. Why no one explains DependencyProperty.Register method and how to use it? What are this list of arguments you passed to the Register method ("MyProperty", typeof(string), typeof(CarDependencyProperty)? Why no one explains DependencyProperty class, its members and how to use it? What's the purpose of MyCar property? Where do you get GetValue and SetValue methods from? What are they doing, exactly? "They get and set value" duh. But where do they get and set what value, exactly?? There are like 20 questions that need to be answered in order to understand that DependencyProperty object and how to use it but no one answers them and talks about this DependencyProperty object like everyone who's reading already knows all this information. But if I knew all this information beforehand, I wouldn't search and open your article in the first place. In pre-requisites you said "Before doing that, I hope you are familiar with some basic knowledge of the CLR, Object-Oriented Programming and have made at least one simple WPF application". Then you start your application with Window.Resources and continue with XAML extensions and data binding. That's far from "at least one" and "simple WPF application". That's advanced stuff that you don't learn in first chapters of books. People start using XAML extensions either at the end of intro C#/WPF/XAML books(few tenths of simple applications) or in the intermidiate level books(Mastering WPF, Design Patterns books or alike).
Former memberPosted Jan 5, 2019, 1:53 AM
Clear explanation. Thanks for it
Pradeep SPosted Jun 22, 2018, 5:42 AM
What is CarDependency in 1st line of MyButton_Click(object sender, RoutedEventArgs e)?
Sanwar RanwaPosted Jan 20, 2018, 1:04 AM
Good work keep it up
Vj MehtaPosted Mar 2, 2017, 5:45 AM
NOT completed , you should explain with complete example ,what you have shared is half.
chandru chandruPosted Jul 16, 2016, 1:51 AM
Not helpful
Hari KommalapatiPosted Mar 15, 2016, 6:00 AM
public class CarDependencyProperty : DependencyObject { //Register Dependency Property public static readonly DependencyProperty CarDependency = DependencyProperty.Register("MyProperty", typeof(string), typeof(CarDependencyProperty)); public string MyCar { get { return (string)GetValue(CarDependency); } set { SetValue(CarDependency, value); } } } in XAML.cs file private void MyButton_Click(object sender, RoutedEventArgs e) { CarDependencyProperty dpSample = TryFindResource("CarDependency") as CarDependencyProperty; MessageBox.Show(dpSample.MyCar); } in XAML file <Window.Resources> <local:CarDependencyProperty x:Key="CarDependency"></local:CarDependencyProperty> </Window.Resources> <Grid> <Grid.RowDefinitions> <RowDefinition /> <RowDefinition /> </Grid.RowDefinitions> <Label Content="Enter Car:" Grid.Row="0" VerticalAlignment="Center" /> <TextBox Text="{Binding Path=MyCar, Source={StaticResource CarDependency }}" Name="MyTextCar" Height="25" Width="150" HorizontalAlignment="Right" Margin="0,18,165,18" /> <Button Name="MyButton" Content="Click Me!" Height="25" Click="MyButton_Click" Width="150" Grid.Row="1" /> </Grid>
Hari KommalapatiPosted Mar 15, 2016, 6:00 AM
Demo will work with below code changes . Copy below code into noted pad and then paste into XMAL file
ankur goyalPosted Feb 24, 2016, 9:33 AM
Complete sample code is so confusing, its better that please don't post the code if it is not correct.
ashish sajwanPosted Nov 30, 2015, 9:46 AM
I think instead of DependencyPropertySample in DependencyProperty ,we have to use typeof(CarDependencyProperty)
Pitambar SahooPosted Aug 29, 2015, 4:55 AM
Can you please explain about "DependencyPropertySample"? I tried and not getting information about "DependencyPropertySample" . Please explain...
Mohit AgarwalPosted Jul 17, 2015, 2:53 AM
"DependencyPropertySample" , where this class is defined?
chandan tiwariPosted Apr 26, 2015, 10:45 AM
nice article but it leaves many of the important concept of DP
Arunava BhattacharjeePosted Sep 8, 2014, 12:46 AM
Thought of posting an article on Dependency Property...but this is great and I dropped the idea...wonderful post...keep posting :)
kalu singh raoPosted Aug 28, 2014, 12:45 AM
it is not working...
sushil mishraPosted Aug 21, 2014, 5:47 AM
Hi Deepika, i have tried to follow you code but i am not getting what is DependencyPropertySample. please clarify
Shweta LodhaPosted Apr 12, 2014, 2:06 PM
Nice writeup
Kiran Kumar TalikotiPosted Aug 27, 2013, 12:06 AM
Nice article:)
Karuppasamy PandianPosted Jul 26, 2013, 5:23 AM
a nice Article
Sam HobbsPosted Jul 24, 2013, 1:15 PM
Also note that in the sample code, I do not understand how the XAML is related to the Code Behind. In the XAML is "CarDependency" twice but I do not see how that is related to the CarDependencyProperty class. I see that the CarDependencyProperty class has a member called CarDependency; is that how the XAML is related to the Code Behind? If the local:DependencyPropertySample resource, StaticResource CarDependency Source of the TextBox and the CarDependency member of the CarDependencyProperty are all related then it would really help to explain how.
Sam HobbsPosted Jul 24, 2013, 1:01 PM
Welcome to C# Corner. Thank you for explaining Dependency Properties, I was not familiar with the definition of the term. You explained it well except there is one thing that I think it would help if it was clarified. You say that a Dependency Property is "in a dictionary of keys and values provided by the base class DependencyObject". It is not clear to me however where that is.
Akhil MittalPosted Jul 24, 2013, 8:51 AM
Nice Artcle,Welcome to c-sharp corner.
Dinesh BeniwalPosted Jul 24, 2013, 3:03 AM
Good start deepika welcome to the c# corner