No-nonsense WPF Series 12: dependency attributes and additional attributes

Source: Internet
Author: User
Tags mscorlib

I. Dependency attributes

The dependency property means that you do not have a value. The value obtained from the data source through binding is dependent on others. Objects with dependency properties are called dependent objects.

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

When the dependent object is created, it does not contain the storage space. In WPF, the dependency object must be used as the host of the dependency attribute.

 

Declare and use dependency attributes

Tip: You can use this function when creating dependency properties.CodeSegment propdp

XAML

 

 
   
    
     
     
     
     
    
   background Code 
Using system; using system. collections. generic; using system. windows; using system. windows. data; using system. windows. events; namespace deepxaml {public partial class mainwindow: window {public mainwindow () {initializecomponent (); game = new game {Title = "Warcraft"}; this. stackpanel. datacontext = game ;}} public class game: dependencyobject {Public String title {get {return (string) getvalue (nameproperty) ;}set {setvalue (nameproperty, value );}} // using a dependencyproperty as the backing store for name. this enables animation, styling, binding, Etc... public static readonly dependencyproperty nameproperty = dependencyproperty. register ("title", typeof (string), typeof (game ));}}

Dependencyproperty. Register Method explanation

A. the first parameter specifies which CLR attribute is used as the packaging of the dependency attribute.

B. The second parameter specifies the type of the retention period of the dependency attribute.

C. Type of the dependent attribute host.

Where is the value of the dependency attribute?

During the running of WPF, a global hashtable access dependency attribute value is maintained.

 

Ii. Additional attributes

You do not have this additional attribute and need to append it in some contexts. For example, the grid. Row attribute of textbox is meaningless if we define a row attribute when defining the textbox class, because we do not know that it will definitely be placed in the grid, which leads to waste.

 

<Window X: class = "deepxaml. mainwindow "xmlns =" http://schemas.microsoft.com/winfx/2006/xaml/presentation "xmlns: x =" http://schemas.microsoft.com/winfx/2006/xaml "xmlns: Local =" CLR-namespace: deepxaml "xmlns: SYS =" CLR-namespace: system; assembly = mscorlib "Title =" mainwindow "Height =" 250 "width =" 450 "> <grid. rowdefinitions> <rowdefinition Height = "2 *"> </rowdefinition> </grid. rowdefinitions> <textbox grid. row = "0"> </textbox> <textbox grid. row = "1"> </textbox> </GRID> </WINDOW>

In fact, after compilation, grid. Row will generate the setrow method in the grid class, and grid will adjust the control of the control. That is, the method generated by the additional attributes does not occupy the memory space.

Public class gameap: dependencyobject {public static int getname (dependencyobject OBJ) {return (INT) obj. getvalue (nameproperty);} public static void setname (dependencyobject OBJ, int value) {obj. setvalue (nameproperty, value);} // using a dependencyproperty as the backing store for name. this enables animation, styling, binding, Etc... public static readonly dependencyproperty nameproperty = dependencyproperty. registerattached ("name", typeof (INT), typeof (gameap), new uipropertymetadata (0 ));}
 
 

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.