[WPF Basics] [5] _ attribute System

Source: Internet
Author: User

Windows Presentation Foundation (WPF) provides a set of services that can be used to extend the functionality of the Common Language Runtime Library (CLR) attributes. These services are generally referred to as the WPF property system. The property supported by the WPF property system is called the Dependency Property. This section describes the WPF property system and the functions of dependency properties, including howProgramMarkup Language (XAML) neutralizationCodeUse existing dependency properties. This section also describes the unique aspects of dependency properties (such as dependency property metadata) and describes how to create your own dependency properties in a custom class.

Prerequisites
This topic assumes that you have some basic knowledge in Clr and object-oriented programming. To use the examples in this topic, you should also understand XAML and know how to write a WPF application.

Dependency attributes and CLR attributes
In WPF, attributes are usually published as Common Language Runtime Library (CLR) attributes. At the basic level, you can directly interact with these attributes without knowing they are implemented as dependency properties. However, you must be familiar with some or all of the functions of the WPF property system before using these functions.

The purpose of the dependency attribute is to provide a method to calculate the attribute value based on other input values. These other inputs can include system attributes (such as themes and user preferences), real-time attribute determination mechanisms (such as data binding and animation/demo panels), and reuse templates (such as resources and styles) or the value is exposed through the parent-child relationship with other elements in the element tree. In addition, you can implement dependency properties to provide independent verification, default values, monitoring callback for changes to other attributes, and systems that can force attribute values to be specified based on possible runtime information. A derived class can also modify some specific features of an existing property by overwriting the metadata of the Dependency Property (instead of overwriting the actual implementation of an existing property or creating a new property.

In the SDK reference, you can determine whether a property is a dependency attribute based on whether the "Dependency Property Information" section exists on the managed reference page of an attribute. The "Dependency Property Information" section contains a link to the dependencyproperty Identifier Field of the Dependency Property, it also includes a list of metadata options set for this attribute, rewriting information for each class, and other details.

Dependency attributes support CLR attributes.
The Dependency Property and WPF property system extend the property function by providing a type that supports the property. This is an alternative to the standard mode that supports the property using private fields. The type name is dependencyproperty. Another important type for defining the WPF property system is dependencyobject. Dependencyobject defines the base classes that can be registered and have dependency properties.

The following describes the terms used in dependency attributes in this software development kit (SDK) document:

Dependency Property: A property supported by dependencyproperty.

Dependency Property identifier: a dependencyproperty instance, which is obtained as a return value when registering a Dependency Property and stored as a class member. This identifier is used as a parameter in many APIs that interact with the WPF property system.

CLR "packaging": the actual get and set Implementation of the attribute. These implementations use the Dependency Property identifier in the getvalue and setvalue calls to combine the identifier, so that the WPF property system supports the property.

The following example defines the isspinning dependency attribute and describes the relationship between the dependencyproperty identifier and Its supported attributes.

Attribute and the dependencyproperty field naming conventions that support it are very important. A field always has the same name as a property, but a property suffix is appended to it.

Set attribute values
You can set attributes in code or XAML.

Set attribute values in XAML
In the following XAML example, the background color of the button is set to red. This example demonstrates the following situation: in the generated code, The XAML loader converts the type of the simple string value of The XAML attribute to the WPF type (a color, using solidcolorbrush ).

XAML supports syntax formats for setting attributes. The syntax used for a specific attribute depends on the value type used by the attribute and other factors (for example, whether a type converter exists ).

As an example of non-attribute syntax, the following XAML example shows another button background. This time, instead of setting a simple solid color, we set the background to an image, represent the image with an element, and specify the source of the image as the attribute of the nested element. This is an example of the attribute element syntax.

Set attributes in code
In code, setting dependency attribute values is usually only called by the set implemented by the CLR "encapsulated" public.

Retrieving attribute values is actually using get "Wrap:

You can also directly call the property system API getvalue and setvalue. If you are using an existing property, the above operations are generally not necessary (it is easier to use the packaging and make the property more accessible to the developer tools ). However, in some cases, APIs can be called directly.

You can also set attributes in XAML and then access these attributes through code hidden in the code.

Attribute functions provided by dependency properties
The Dependency Property provides the function to extend the property function, which is opposite to the property supported by the field. Each of these features usually represents or supports specific functions in the entire WPF function:

Resources

Data Binding

Style

Animation

Metadata Rewriting

Property Value inheritance

WPF Designer Integration

Resources
The dependency attribute value can be set by referencing a resource. Resources are usually specified as the page root element or child element of the application. These locations allow you to access resources most conveniently. The following example shows how to define a solidcolorbrush resource.

After defining a resource, you can reference the resource and use it to provide the property value:

This specific resource is called the dynamicresource tag extension (static or dynamic resource reference can be used in XAML ). To use dynamic resource reference, you must set it to the dependency attribute. Therefore, it is a dynamic resource reference usage that is explicitly enabled by the WPF property system.

Note:
The resource is regarded as a local value, which means that if you set another local value, the resource reference will be eliminated.
 
Data Binding
The dependency attribute can be referenced by data binding. Data Binding works with a specific markup extension syntax (in XAML) or binding object (in code. When data binding is used, the final attribute value will be delayed to the runtime, and the attribute value will be obtained from the data source at runtime.

The following example uses a binding in XAML to set the content attribute for the button. This binding uses an inherited data context and an xmldataprovider data source (not displayed ). The binding itself specifies the required source attributes through XPath in the data source.

Binding is regarded as a local value, which means that if you set another local value, the binding will be eliminated.

The dependency attribute or dependencyobject class does not support inotifypropertychanged, so as to generate notifications about changes in dependencyobject source attribute values for the Data Binding operation.

Style
Styles and templates are two main ways to use dependency attributes. Styles are especially useful when setting properties that define the Application User Interface (UI. A style is usually defined as a resource in XAML. Styles interact with the property system because they usually contain the "setter" of a specific property and the "Trigger" of changing the property value based on the real-time value of another property ".

The following example creates a very simple style (which will be defined in the resources dictionary and not displayed), and then applies the style directly to the style attribute of the button. The setter in the style sets the background attribute of the button with the style to green.

Animation
You can animation the dependency attributes. When an animation is applied or executed, the operation Priority of the animation processed value is higher than that of the property in any other way (such as a local value ).

In the following example, the background of the button attribute is animated (technically, the background uses the attribute element syntax to specify the blank solidcolorbrush as the background for animation processing, the color attribute of this solidcolorbrush is the attribute for direct animation processing ).

Metadata Rewriting
When deriving from a class that initially registers a Dependency Property, You can override the metadata of the Dependency Property to change certain behaviors of the property. Rewriting of metadata depends on the dependencyproperty identifier. You do not need to re-implement attributes to rewrite metadata. Metadata changes are processed by the Property System on the local machine. For all attributes inherited from the base class, each class may reserve metadata based on each type.

The following example overwrites the metadata of the defaultstylekey attribute. Rewriting the metadata of a specific dependency attribute is part of an implementation mode. This mode creates controls that can use the default style in the topic.

Property Value inheritance
An element can inherit the value of the dependency attribute from its parent level in the tree.

Note:
Property Value inheritance is not globally enabled for all dependency attributes, because the inherited computing time does have a certain impact on performance. Attribute Value inheritance is generally enabled only when a specific scheme specifies that property value inheritance is applicable. You can check the "Dependency Property Information" section of a dependency property in the SDK reference to determine whether the Dependency Property inherits the property value.

The following example shows a binding and sets the source datacontext attribute of the specified binding (not shown in the previous binding example. The value of the datacontext attribute is inherited, so any subsequent binding in the child element does not have to follow the source specified as datacontext in the parent stackpanel element.

WPFDesigner Integration
If a custom control has properties that implement the dependency attribute, it receives support from the Visual Studio Windows Presentation Foundation (WPF) designer. In one example, you can edit the direct and additional dependency attributes in the "properties" window.
Priority of Dependency Property Values
When you obtain the value of the Dependency Property, you may obtain the value set on the property through any other input that participates in the WPF property system and is based on the property. Because there is a dependency attribute value priority, various methods for obtaining the value of a property can interact in a predictable manner.

See the following example. This example includes a style applied to all buttons and their background attributes. However, a button with the background value set locally is also specified.

Note:
The SDK documentation sometimes uses terms such as "local value" or "locally set value" when discussing dependency item properties. The value set locally is the property value directly set for the object instance in code, or the property value set as the element attribute in XAML.

In fact, this attribute is set twice for the first button, but only one value is applied, that is, the value with the highest priority. The locally set value has the highest priority (except for running animations, but no animation is applied in this example). Therefore, the locally set value is used for the background of the first button, instead of using the style setter value. The second button does not have a local value (and there are no other values with a higher priority than the style setter), so the background in this button will come from the style setter.

Why is there a dependency attribute priority?
In general, you do not want to always apply styles, and do not want styles to cover even a local set value of a single element (otherwise, it is usually difficult to use styles or elements ). Therefore, the Operation priority from the style value is lower than the value set locally.
Note:
The WPF element defines many attributes of non-dependency attributes. Generally, only when you need to support at least one scheme enabled by the Property System (data binding, style, animation, default value support, inheritance, additional property or failure, only when the property is implemented as a dependency property.

Learn more about dependency properties
An additional property is a type of property that supports Special syntax in XAML. Generally, additional attributes do not have a relationship with the Common Language Runtime Library (CLR) attributes, and they are not necessarily dependency attributes. The typical purpose of an additional attribute is to enable the child element to report the attribute value to its parent element, even if the parent element and the child element's class member list do not have this attribute. A major solution is to enable the child element to notify the parent of its representation in the UI.
Component developers or application developers may want to create their own dependency attributes to enable functions such as data binding or style support, or to support invalidation and force specified values.
Generally, the dependency attribute should be regarded as a public attribute, which can be accessed by any caller with instance access permissions or at least discovered by such caller.

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.