WPF Dependency Property

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

  1. WPF DEPENDENCY PROPERTY
  2. DEPENDENCY PROPERTY; WHAT IS IT?
  3. CLR PROPERTY VS. DEPENDENCY PROPERTY
  4. ADVANTAGES OF DEPENDENCY PROPERTY
  5. EXAMPLE
  6. 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.

Dependency Property 

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. 

Whereas when you set a value of a Dependency Property it is not stored in a field of your object, but in a dictionary of keys and values provided by the base class DependencyObject. The key of an entry is the name of the property and the value is the value you want to set.

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

  1. <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">  
  2.     <Window.Resources>  
  3.         <ResourceDictionary>  
  4.             <local:CarDependencyProperty x:Key="CarDependency"></local:CarDependencyProperty>  
  5.         </ResourceDictionary>  
  6.     </Window.Resources>  
  7.     <Grid>  
  8.         <Grid.RowDefinitions>  
  9.             <RowDefinition />  
  10.             <RowDefinition />  
  11.         </Grid.RowDefinitions>  
  12.         <Label Content="Enter Car:" Grid.Row="0" VerticalAlignment="Center" />  
  13.         <TextBox Text="{Binding Path=MyCar, Source={StaticResource CarDependency }}" Name="MyTextCar" Height="25" Width="150" />  
  14.         <Button Name="MyButton" Content="Click Me!" Height="25" Click="MyButton_Click" Width="150" Grid.Row="1" />  
  15.     </Grid>  
  16. </Window>  

Code Behind

  1. namespace WpfApplication1 {  
  2.     /// <summary>  
  3.     /// Interaction logic for DependencyPropertyDemo.xaml  
  4.     /// </summary>  
  5.     public partial class DependencyPropertyDemo: Window {  
  6.         public DependencyPropertyDemo() {  
  7.             InitializeComponent();  
  8.         }  
  9.         private void MyButton_Click(object sender, RoutedEventArgs e) {  
  10.             CarDependencyProperty dpSample = TryFindResource("CarDependency") as CarDependencyProperty;  
  11.             MessageBox.Show(dpSample.MyCar);  
  12.         }  
  13.     }  
  14.     public class CarDependencyProperty: DependencyObject {  
  15.         //Register Dependency Property  
  16.         public static readonly DependencyProperty CarDependency = DependencyProperty.Register("MyProperty"typeof(string), typeof(CarDependencyProperty));  
  17.         public string MyCar {  
  18.             get {  
  19.                 return (string) GetValue(CarDependency);  
  20.             }  
  21.             set {  
  22.                 SetValue(CarDependency, value);  
  23.             }  
  24.         }  
  25.     }  
  26. }  

 

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.