WPF Depedency Properties

Kailash Chandra Behera | Thursday, May 19, 2016


This article describes about WPF dependency proprties with example and it also explains how dependency proprties are difference from normal CLR proprties

Getting Started

A Dependency Property is a property whose value depends on the external sources, such as animation, data binding, styles, or visual tree inheritance. The Dependence property extends the CLR property, which means it provides some external service to CLR property like it is providing notification when the property has changed, data binding, and styling.

A class which defines a dependency property must be inherited from the DependencyObject class. Many of the UI controls class which are used in XAML are derived from the DependencyObjectclass and they support dependency properties, e.g. Button class supports the IsMouseOver dependency property.

Need of Dependency Property

Dependency Properties offer a lot of functionalities that you won’t get by using a CLR property. We can use Dependency property for the following reason

  • If you want to set the style
  • If you want data binding
  • If you want to set with a resource (a static or a dynamic resource)
  • If you want to support animation

Difference between Dependency Property and CLRR property

Dependency properties are stored in a dictionary of key/value pairs which is provided by the DependencyObject class. It also saves a lot of memory because it stores the property when changed. It can be bound in XAML as well. But CLR properties can directly read/write from the private member of a class by using getter and setter. In contrast, dependency properties are not stored in local objects.

Dependency property support notification, means whenever a property changes its value it provides notification in the Dependency Property using INotifyPropertyChange and also helps in data binding, whereas CLR Property does not support any notification

A Dependency Property can animate, set styles using style setters, and even provide templates for the control. Whereas CLR property does not support anything.

Dependency property also support callback feature, but CLR Property does not.

Advantages of a Dependency Property

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

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

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

  4. Participation in animation, styles and templates

    A Dependency Property can animate, set styles using style setters and even provide templates for the control.

  5. CallBacks

    Whenever a property is changed you can have a callback invoked.

  6. Resources

    You can define a Resource for the definition of a Dependency Property in XAML.

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

 <Window x:Class="WPFDependencyProperty.MainWindow"   
     Title="MainWindow" Height="350" Width="604">   
     <Button Height="40"   
          Content="Dependency Property">   
         <Style TargetType="{x:Type Button}">   
             <Trigger Property="IsMouseOver" Value="True">   
               <Setter Property="Foreground" Value="Red"/>   

Related Articles

  1. Resource in WPF
  2. UpdateSourceTrigger
  3. WPF Binding
  4. WPF Style
  5. WPF Converters
  6. WPF MultiValue Converter
  7. WPF Value Converters
  8. WPF INotifyPropertyChanged Interface


In the above discussion, hope you have got idea about Dependency property and how it is different from CLR property.