Dependency Property
If you are familiar with the .NET framework, you already know what a Common Language Runtime (CLR) property is: it is the classic .NET class property. In WPF, we specifically refer to them as CLR properties mainly to distinguish them from the new Dependency Property.
In short, a Dependency Property is a special form of property that has the feature of being able to interact with WPF (styling, data binding, animation, etc.).
A Dependency Property is always a property of an object derived from the Dependency Object class. Note that only Dependency Properties can be used as a binding target. A Dependency Property is created using the static Register method of the Dependency Property class.
Dependency Object
The Dependency Object class provides all the functionality of the WPF dependency property system. Most of the WPF classes derive directly or indirectly from Dependency Object.
Attached Dependency Property
An Attached Dependency Property (also called attached property for simplicity) is a Dependency Property which can be set and read to/from any instance of any Dependency Object (not limited to the declared owner types, as with a regular Dependency Property).
With this approach, some classes can interact with objects that know nothing about these classes.
A perfect example of attached properties is the Grid class.
The Grid class defines two attached properties: Grid.Row and Grid.Column.
These two attached properties can be used to alter the layout of controls nested under the Grid control without having them know a thing about the Grid class or how the layout is performed in the Grid class.
Why is DependencyProperty static?
The declaration of DependencyProperty is static not its value (i.e the memory storage). The declaration that you add with static keyword is just the identifier of the DependencyProperty for the DependencyObject because the same identifier will be shared by all the instances of that DependencyObject to identify the property hence it makes sense to make it static.
On the other hand, onwhen you set the value of DependancyProperty by calling the SetValue DependancyObject instance, then each instance of DependancyObject on which the setvalue is called will store its local value of the Property. This is handled internally by the DependancyObject class which maintain sort of Dictionary which has the mapping between the DependancyProperty identifier and the local value.
How to define a dependencyProperty?
Below are the steps to create dependency property
1) Defining a Dependency Property
2) Registering a Dependency Property
3) Adding a property wrapper
public class MyStateControl : ButtonBase
{
public MyStateControl() : base() { }
public Boolean State
{
get { return (Boolean)this.GetValue(StateProperty); }
set { this.SetValue(StateProperty, value); }
}
public static readonly DependencyProperty StateProperty = DependencyProperty.Register(
ean), type "State", typeof(Boolof(MyStateControl),new PropertyMetadata(false));
}
Summary for Dependency property
- Dependency Property System holds a collection of all DependencyProperty with its corresponding ownerType.
- Each instance of a DependencyObject holds a collection of all individual DependencyProperty that has been changed either through animation or programmatically.
- Styles produce a separate entity that holds a Key Value collection of a DependencyProperty and its value, once it is applied to a FrameworkElement, it modifies the default DependencyProperties with the ones that is defined inside styles.
- Separating DependencyProperty from the object instance saves lots of space, as Reference Types will not create object instances when each instance is created, but will use up the existing instance every time. The separation of property system also allows having a Property to be used as attached to its children.
- The main difference is, that the value of a normal .NET property is read directly from a private member in your class, whereas the value of a DependencyProperty is resolved dynamically when calling the GetValue() method that is inherited from DependencyObject.
- 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 DndeepencyObject. The key of an entry is the name of the property and the value is the value you want to set.An excellent link for internals of dependency propertieshttp://www.abhisheksur.com/2011/07/internals-of-dependency-property-in-wpf.html
No comments:
Post a Comment