public string Name { get; set; }
private string _name;
public string Name
{
get { return _name; }
set { _name = value; }
}
This paradigm allows you to do validation on the setting of the backing field or sanitation on the getting of the backing field.
Now there is a drawback to such properties, and that is that the take up memory, not a big deal right? well let's say you have a control that has 50 properties, each on of them takes up memory, so if you now have multiple instances of this control (let's say a button or a textbox) well then that's 50 times however many instances of that control you have on your form; as you can imagine these properties will chew through your memory. Worse yet how many of these properties do you change from their default values, 3? maybe 5?
In come Dependency Properties, you can think of dependency properties as static if they are left in their default state, that means for example: all buttons share the IsEnabled property if it's left in it's default value of true, only when the value is changed on a control is the dependency property stored.
Any class that wants to use a dependency property must inherit from DependencyObject, inside of this baseclass there exists the mechanism that stores all of the customized values in a key value dictionary, Internally when you call the GetValue(DependencyProperty) function on a dependency property first the dictionary is checked for a set local value, if one does not exist, the mechanism walks up the logical tree looking for an inherited value (ie something like font) then finally if it walks all the way up the logical tree it takes the default static value of the dependency property.
As mentioned Dependency Properties allow controls to use inheritance; often when you set properties such as font or color you want them to be inherited by the controls children, as mentioned if no defined local value exists for a Dependency Property it will next try to find the inherited value before defaulting to the static one.
Possibly the most advantageous benefit of Dependency Properties is Change Notification, when you declare a Dependency Property you pass in a callback that notifies you if the value is changed, this is utilized by DataBinding.
In Summary Dependency Properties bring Three Main advantages to the Table
- Change Notification: mainly used by animations to change values
- Reduced Memory Footprint: Static default values, allow you to share backing fields
- Value Inheritance: allow dependency Properties to inherit values from parent controls.
Let's take a deeper look, now remember when I said that Dependency properties resolve to their local value, inherited value and then their default value? we'll I left 7 other things they could resolve to:
- Animation: the current animation that is being executed on your control.
- Bound Value: the corresponding property bound to in a ViewModel
- Local Value: a local value that is set in the xaml
- Template Properties: values set in a control template
- Custom Style Setter: a style that is specified in the resources of either your page or app
- Default Style Trigger
- Default Style Setter: A value that is defined at: "C:\Program Files (x86)\Windows Kits\8.1\Include\winrt\xaml\design"
- Inherited Value: A value from a parent control that has the same Property
- Default Value: the value specified when the Property is Registered
public int MyProp
{
get { return (int)GetValue(MyPropProperty); }
set { SetValue(MyPropProperty, value); }
}
// Using a DependencyProperty as the backing store for MyProp. This enables animation, styling, binding, etc...
public static readonly DependencyProperty MyPropProperty =
DependencyProperty.Register("MyProp", typeof(int), typeof(Exercise),new PropertyMetadata(0));
- The first parameter is the key associated with the dependency property it has to match the accessor Property name,
- the second is the type that the property represents,
- the third is the class that the dependency property is in,
The third property is where the action happens, it has 5 constructors PropertyMetadata basically it has three potential parameters:
- Object: a default value for the Dependency property
- PropertyChangedCallback: a delegate that get's called every time the property is changed.
- CoerceValueCallback: allows you to set a boundary of values for your dependency properties