What is a dependency attribute? (Problems encountered when practicing StoryBoard), storyboard story

Source: Internet
Author: User

What is a dependency attribute? (Problems encountered when practicing StoryBoard), storyboard story
Overview:

Windows Presentation Foundation (WPF) provides a set of services that can be used to extend the functionality of Common Language Runtime (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.

The Dependency Property provides the function to extend the property function. The most common feature is the "Data Binding" function. The dependency property 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 is determined to be delayed to the runtime. During the runtime, the attribute value is obtained from the data source and can be dynamically updated.

Purpose:

This section describes the application scenarios of dependency attributes and the methods for customizing dependency attributes.

Body: What is Dependency Property?

Dependency properties support style settings, data binding, inheritance, animation, and default values in WPF. The dependency property is registered in the WPF Property System by calling the Register Method (or RegisterReadOnly) and marked with the DependencyProperty identifier. Dependency attributes can only be used by types inherited from the DependencyObject class, but DependencyObject has a high level in the WPF class hierarchy. Therefore, most available classes in WPF support dependency attributes.

When to implement dependency attributes

Setting all attributes as dependency properties is not always the correct solution, depending on the application scenario. Sometimes, the typical method of using private fields to implement attributes can meet the requirements. MSDN provides the following application dependency attributes:

1. You want to set attributes in the style.

2. You want the property to support data binding.

3. You want to use dynamic resource reference to set attributes.

4. You want to automatically inherit the attribute value from the parent element in the element tree.

5. You want the attributes to be animated.

6. report if you want the property system to modify the value of the property before performing an operation or reading and using the style in the property system, environment, or user.

7. You want to use the metadata conventions that have been established and used by the WPF process, for example, whether the layout system is required to rewrite the Visual Object of the element when the report changes the attribute value.

Currently, the most common scenarios are the first three. Below is a typical application scenario of dependency attributes in Data Binding:

There are two images A and B on the interface. The size is the same. We need to change the size of Image B while changing the size of image A to keep them consistent. What should we do as usual?

We need to add the method for processing image B in the SizeChanged event of image. Assign the Size of image A to image B. It is not complicated to use such a piece of code for such a simple function. If there are more such requirements, more events will be handled.

However, you can use the following three statements to implement the dependency attribute:

imageB.DataContext = imageA;imageB.SetBinding(Image.WidthProperty, "Width");imageB.SetBinding(Image.HeightProperty, "Height");

 

How about it? concise enough! The general meaning of this passage is:

1. Set the data context of ImageB to ImageA when binding data, that is, the data bound to ImageB is searched on ImageA;

2. Bind the Width and Height of ImageB to the "Width" and "Height" attributes through the SetBinding method. Find the values of these two attributes in DataContext of ImageB, where they are "Width" and "Height" of ImageA ".

In this way, the "Width" and "Height" of ImageA and ImageB are bound together. When the "Width" and "Height" of an image change, the Size of another image will also change accordingly.

How to customize dependency attributes

 

When attributes are implemented on a class, as long as the class is derived from DependencyObject, you can use the DependencyProperty identifier to identify the attributes and set them as dependency properties. The syntax is as follows:

Public static DependencyProperty TextProperty = DependencyProperty. register ("Text", // The property name typeof (string), // The property type typeof (TestDependencyPropertyWindow), // the property owner, register this property to the class new PropertyMetadata (""); // The default attribute value is public string Text {get {return (string) GetValue (TextProperty );} set {SetValue (TextProperty, value );}}

We can see that the main difference between it and common attributes is:

1. The field must be static and the type is DependencyProperty. The field name must comply with the naming conventions, that is, the suffix is Property;

2. the modifier of the field must be set to Public; otherwise, the SetBinding method cannot be used externally to bind the value of this attribute;

3. The field value is set through DependencyProperty. Register. For details about each parameter, see the description in the code;

4. When setting attribute values, SetValue and GetValue are used instead of Set and Get.

 

Through the above method, we have customized a dependency attribute "Text" on the CustomSape class, and then we can use this attribute just like using the built-in dependency attribute.

 

In short, dependency attributes are an important feature of WPF. In a few simple words, It is very tedious to implement the original Winform functions. When dependency attributes are used, and when common attributes are used, you must refer to the specific usage and seven application scenarios provided by MSDN.

Reprinted: http://www.cnblogs.com/xiongpq/archive/2010/06/29/1767905.html

Related Article

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.