WPF knowledge Raiders 08- dependency property

I must mention is the WPF dependency property shall not be another series

First look at the custom properties depend on the wording of

public static readonly DependencyProperty IconProperty = 
    DependencyProperty.Register("Icon", typeof(ImageSource), typeof(CustomButton), new PropertyMetadata(null));
public ImageSource Icon
{
    get { return (ImageSource)GetValue(IconProperty); }
    set { SetValue(IconProperty, value); }
}

When defining the properties depend, without the use of typical get {return a;} set {a = value} structure, but the introduction of the SetValue and GetValue, this method two classes from DependencyObject, DependencyProperty type parameter definitions only for static read, he only as an identifier.

1, compared to .NET common attributes, attribute less dependent on private members, more than a dependency property identifier.

2, the value of .NET common attributes are read directly from the class private field, it is to call the value of the GetValue DependencyProperty () dynamically resolved.

3, depending on the value of the property, there is disinterested object storage, is present in a DependencyObject providing dictionary of keys and values.

Dependency property advantages:

Reduce memory consumption

When you think of more than 90% of the property of UI controls generally retain their initial value, is a huge dissipation for each attribute storage field. Dependency property only to solve these problems by storing the modified example of the properties. The default value is stored in a dependency property.

Value is inherited

When accessing a dependency property, the value resolution policy to resolve the value. If the local value is not set, then the dependency property navigation logic tree up until you find the value. When you set up FontSize on the root element, it applies to all the following text blocks, unless you override this.

Change Notification

Dependency properties have a built-in change notification mechanism. By registering callbacks in property metadata, you can be notified when you change the value of the property. This is also the use of data binding.

Reference: https://www.wpftutorial.net/DependencyProperties.html

Guess you like

Origin www.cnblogs.com/kuangxiangnice/p/11067279.html