Dot Net For All

WPF Dependency property with Example

Dependency Property WPF

In this article I will discuss about the Dependency property WPF has been introduced to overcome some of the shortcoming of the regular property system.NET along with the code example in XAML. Going further I will discuss why do we need dependency property. Properties help us to encapsulate the information of the class from the outer world. Though it can be big on the memory footprint for the class.

Why Dependency property are static ?

Suppose I have a class TextBox and I have defined a property known as BackGround. Now whenever I am creating the instance of the class, the memory on the heap will be allocated based on the properties and member variables of the class that will eventually increase the memory footprint for the class. So for example, if we have 100 objects with 100 CLR properties of type int each, then we are using 10000 ints worth of memory even if all those have the same default value (0). This is where dependency properties are relevant.

The Dependency Property has been included in the framework keeping this point in mind. Dependency properties are static in nature. That means that they have some default value stored in the Dependency Property system. The Dependency Property system consists of a dictionary that is popularly known as a “property bag”. This is the same default value that we provide for the property when creating them.

As soon as we are done with this theory, a question comes to our mind that since the dependency property is a static property it means that whenever it is changed at one place the change should be effective at all the places where that property is being used? No but that is not the case, whenever a dependency property is set for an instance of the object for which it is being declared, it stores the value in EffectiveValues collection. It means for each instance which set the value explicitly , the value is stored in the EffectiveValues collection which resides within the DependencyObject class and reserve memory there.

WPF Dependency Property example

We can get the local value for that instance using the ReadLocalvalue() function of the DependencyObject class which is explained here.

As soon as we are clear about this point a question comes to our mind of why they are called “Dependency”. The implementation of these properties depends on the precedence of the value they are getting their values from. As discussed earlier they have some default value assigned when creating them, so that is the last in the hierarchy from which they can get the value. The hierarchy of the sources from which the Dependency Property can get its value are as in the following.

  1. Property system coercion
  2. Active animations, or animations with a Hold behavior
  3. Local value
  4. TemplatedParent template
  5. Style triggers
  6. Template triggers
  7. Style setters
  8. Theme style
  9. Inheritance
  10. Default value from Dependency Property metadata

So from the preceding discussion we can derive the one conclusion, that a Dependency Property WPF doesn’t ever have its own value. Its value always on the number of outside factors or it gets its value from one of the sources in the hierarchy. That’s the reason we call it a “Dependency” property.

That was not all the basics of dependency properties. Apart from that there are many intricacies that we will discuss in a future article.

Implementation with Code Example

Now I want to show you with a code example how a Dependency Property can be used. The basic requirement for having a Dependency Property in your class is that the class should inherit from DepedencyObject that in turns provides the mechanism for storing and retrieving the dependency values.

I have declared a Dependency Property WPF BirthdayYear as follows. Kindly note that whenever we declare a DP .Net framework attached “Property” to the property name then something happens.

public int BirthYear    
{    
   get { return (int)GetValue(BirthYearProperty); }    
   set { SetValue(BirthYearProperty, value); }    
}    
    
// Using a DependencyProperty as the backing store for BirthYear. This enables animation, styling, binding, etc...    
public static readonly DependencyProperty BirthYearProperty =    
DependencyProperty.Register("BirthYear", typeof(int), typeof(BirthdayYear), new PropertyMetadata(1983));

Here I want to state explicitly a few things. As you can see in the preceding listing the DP that I have declared is of type int. The framework uses the Register method to register the DP in the DP property subsystem with the default value 1983. Since it is quite difficult to remember this syntax of declaring dependency properties, the .Net framework provides a code snippet to help us with that. As you will type propdp in the Visual Studio you will get an option to create the DP as shown in the following figure:

Dependency Property WPF

1. After this step you need to press tab twice and you will get the code for declaring a DP. There you need to provide what type your Dependency Property would be, the owner of the Dependency Property. In other words the class in which the property is defined. Now I want to use the User Control that has defined this Dependency Property in my main window. The XAML for which is shown below:

<local:BirthdayYear x:Name="birthdayYear"></local:BirthdayYear> 
<Grid> 
  <Grid.ColumnDefinitions> 
    <ColumnDefinition Width="Auto"> 
    </ColumnDefinition> <ColumnDefinition Width="Auto">
  </ColumnDefinition> </Grid.ColumnDefinitions> 
<TextBlock Text="{Binding BirthYear, ElementName=birthdayYear}" Grid.Column="0" Height="50"></TextBlock> 
<Button Content="Change Value" FontSize="20" Click="OnChangeValue" Grid.Column="1" Height="50"></Button> 
</Grid>

2. Now wherever I run my application my main window will be shown with a textblock that shows the default value of the DP that is “1983”.

3. As shown in Listing 2 I have a Click event for the Button that increases the value of the DP every time it is clicked

private void OnChangeValue(object sender, RoutedEventArgs e)  
{  
   ++birthdayYear.BirthYear;  
}

As soon as the button is clicked the value is increased in the textblock. How does this happen? This has happened using the Dependency Property system that listens to the change notification of the property that has been registered as a DP.

I have attached the code for the project along with this article so that you can play with it.

Kindly provide your feedback about the article covering dependency property WPF.

You may also like to know Attached property in WPF here.

Please download Dependency Property Example code form here

Top career enhancing courses you can't miss

My Learning Resource

Excel your system design interview