WPF Dependency Properties - Windows Presentation Foundation(WPF)

What is WPF Dependency Properties?

In WPF application, dependency property is a particular type of property which extends the CLR property. It takes the advantage of specific functionalities available in the WPF property system.

A class which define a dependency property must be hereditary from the Dependency Object class. Many of the UI control class which are used in XAML are derived from the Dependency Object class and they support dependency properties, e.g. Button class supports theIsMouseOverdependency property.

The following XAML code creates a button with some property.

The x: Type mark-up extension in XAML has a related functionality like type of () in C#. It is used when attribute are specified which take the type of the entity such as <Style Target Type = "{x: Type Button}">

When the above code is compiling and executed, you would get the following Main Window. When the mouse is over the button, it will modify the foreground colour of a button. When the mouse plants the button, it changes back to its original colour.
dependency_property

Why We Need Dependency Properties

Dependency property gives you all kind of benefits when you use it in your appliance. Dependency Property can used over a CLR property in the following scenarios −

  • 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

mostly, Dependency Properties offer a lot of functionalities that you won’t get by using a CLR property.

The main difference between dependency properties and other CLR properties are scheduled below −

  • 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 object.
  • Dependency properties are stored in a dictionary of key/value pairs which is provided by the Dependency Object class. It also saves a lot of memory because it stores the property when changed. It can be bound in XAML as well.

Custom Dependency Properties

In .NET framework, custom dependency properties can also be defined. Follow the steps given below to describe custom dependency property in C#.

  • Declare and register your dependency property with system call register.
  • Provide the setter and getter for the property.
  • Define a static handler which will handle any changes that occur globally
  • Define an instance handler which will handle any changes that occur to that particular instance.

The following C# code defines a dependency property to set the SetText property of the user control.

Here is the XAML file in which the Text Block is define as a user control and the Text property will be assign to it by the Set Text dependency property.

The following XAML code creates a user control and initializes its SetTextdependency property.

Let's run this application. You can instantly watch that in our Main Window, the dependency property for user control has been successfully used as a Text.
dependency_property_for_user

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

Windows Presentation Foundation(WPF) Topics