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 more or less 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 builtin feature of providing notification when the property has changed, data binding and styling.

WPF-Dependency-Property-1.jpg
 
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

<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="150" Width="250">

<Window.Resources>

    <local:DependencyPropertySample x:Key="CarDependency" />

</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" />

    <Button Name="MyButton" Content="Click Me!"

            Click="MyButton_Click" Height="25" 

            Width="150" Grid.Row="1" />

</Grid>

</Window>


Code Behind

public class CarDependencyProperty : DependencyObject

{

    //Register Dependency Property

    public static readonly DependencyProperty CarDependency = DependencyProperty.Register("MyProperty", typeof(string), typeof(DependencyPropertySample));

 

    public string MyCar

    {

        get

        {

            return (string)GetValue(CarDependency);

        }

        set

        {

            SetValue(CarDependency, value);

        }

    }

}

 

public partial class CarDependencyPropertyDemo : Window

{

    public CarDependencyPropertyDemo()

    {

        InitializeComponent();

    }

    private void MyButton_Click(object sender, RoutedEventArgs e)

    {

        CarDependency dpSample =

        TryFindResource("CarDependency ") as CarDependency;

        MessageBox.Show(dpSample.MyCar);

    }

}

WPF-Dependency-Property-2.jpg

WPF-Dependency-Property-3.jpg

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.

Up Next
    Ebook Download
    View all
    Learn
    View all