Start networking and exchanging professional insights

Register now or log in to join your professional community.

Follow

What is the difference between CLR property and Dependency Property in WPF ?

user-image
Question added by Abhijeet Saxena , Software Engineering Analyst , Accenture Services Pvt. Ltd
Date Posted: 2016/09/04
Amit Sharma
by Amit Sharma , Systems Analyst , TCS

CLR property is just a wrapper around private variables. It uses Get / Set methods to retrieve and store value of a variable into it. A CLR property gives you only one block in which you can write code to invoke whenever a property is get or set.

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 Dependency Object. The key of an entry is the name of the property and the value is the value you want to set.

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.

To work with dependency property, you must derive your class from Dependency Object as the whole observer which holds the new Property System is defined within the Dependency Object.

E.g.

public static readonly DependencyProperty TestProperty =

DependencyProperty.Register("Test", typeof(string), typeof(Window1));

public string Test

{

    get

    {

        return this.GetValue(TestProperty) as string;

    }

    set

    {

        this.SetValue(MyCustomProperty, value);

    }

}

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

More Questions Like This

Do you need help in adding the right keywords to your CV? Let our CV writing experts help you.