1. Dependency attributes
The biggest feature of the Dependency attribute is the built-in change notification function.
The Dependency attribute function is mainly used to provide rich functions when declaring tags. The key to the friendly design declared by WPF is the extensive use of attributes. If there is no Dependency attribute, We will write a lot of code to implement the attribute display function.
1. Change Notification: after the property value is changed, the notification page is updated.
2. Property Value inheritance function: child elements inherit the value of the corresponding property name in the parent element.
3. Support multiple objects: You can set the value of the Dependency attribute in multiple ways.
Let's take a look at an example:
Public class Button: ButtonBase
{
// Declare a dependency static attribute
Public static readonly DependencyProperty IsDefaultProperty;
Static Button ()
{
// Register this property to the Button
Button. IsDefaultProperty = DependencyProperty. Register ("IsDefault", typeof (bool), typeof (Button ),
New FrameworkPropertyMetadata (false,
New PropertyChangedCallback (OnIsDefaultChanged )));
...
}
// Default attribute
Public bool IsDefault
{
Get {return (bool) GetValue (Button. IsDefaultProperty );}
Set {SetValue (Button. IsDefaultProperty, value );}
}
// Method to be called when the attribute is changed
Private static void OnIsDefaultChanged (
Dependencyobject o, dependencypropertychangedeventargs e ){... }
...
}
In the above implementation code, the static field isdefaultproperty represented by the system. Windows. dependencyproperty class is true.
Dependency attribute. For convenience, all dependency attributes are public and static, and there are also attribute suffixes. The dependency attribute is usually created and available for static use.
Method dependencyproperty. Register. Parameter attribute name, type, class using this attribute. In addition, other notification event handling and default values can be provided based on the overload method.
To be continued ............