WPF Study Notes-Introduction to xaml attributes

Source: Internet
Author: User

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 ............

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.