Skip to main content

WPF Dependency Properties


This article describes WPF dependency properties with example and it also explains how dependency properties are different from normal CLR properties.

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, sets 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. WPF Style
  2. WPF Resource
  3. WPF Binding


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



Popular posts from this blog

Generate QR Code in WPF

Introduction In my previous two blogs, we have discussed how to display generate and display various barcodes on the web page. In this blog, we are going to demonstrate how to Generate QR Code in the WPF application. Getting Started Here in the demonstration Generate QR Code in WPF , will generate QR code using third party library as there is not inbuilt library provided by Microsoft to generate QR code and will display in WPF image control, In the below, we will see the steps to display QR Code. As I mentioned in the above paragraph that there is no inbuild library provided by Microsoft to generate QR code, I have taken the help of the ZXing library which is a third party free library and available Nuget. This library provides various options to generate barcodes and QR Code from the user-friendly text. Generate QR Code in WPF Here are the steps to generate QR code and let's follow the steps to complete demonstrations. Demonstration:- Generate QR Code Open visual stud

Creating Application in Prism

Introduction This article explains an illustration of creating a windows application in Prism Library(WPF Prism). The solution includes recommended practices and techniques and is the basis for the procedures in Prism. This illustration created in the Visual Studio 2012, It can also developed in the visual studio 2008 and 2010, because wpf supports from .net framework 3.5 to latest version. Microsoft.Practices.Prism.dll. This assembly contains the implementation of the Prism Library core components such as modularity, logging services, communication services, and definitions for several core interfaces. It also contains the implementation of Prism Library components that target WPF applications, including commands, regions, and events. Microsoft.Practices.Prism.UnityExtensions.dll. This assembly contains base and utility classes you can reuse in applications built with the Prism Library that consume the Unity Application Block. For example, it contains a bootstrapper base class

WPF Binding

Introduction This blog describes WPF Binding and the measure elements exist with binding with XAML example in XAML and code-behind. Getting Started Binding helps in WPF to flow data from one object to another object, the object which fetches data is called source and the object which receives the data is called target. The Object a be a UI control or object of a class that means in binding you can bind a property of a class and property of another control to WPF UI controls as well. XAML Example <TextBox x:Name="sourceText" Grid.Row="0" /> <Button Grid.Row="1" Content="{Binding ElementName=sourceText, Path=Text}"/> Code Example Button btn=new Button(); Binding binding = new Binding("Text"); binding.Source = sourceText; btn.SetBinding(Button.ContentProperty, binding); In WPF binding has some measure properties or elements that we are using while developing application or projects, here we are going to d