Learning Notes 2 binding, dependency attributes, and additional attributes

Source: Internet
Author: User
Chapter 6 in simple terms: Binding 1. The binding source can be any object and its own data can be disclosed through attributes. The binding target must be the dependency attribute of the dependent object.

2. inotifypropertychanged interface. When the object implements this interface, when the data source changes, the UI can be notified to implement changes at the same time. Implementation principle: After the data source is set for binding, binding automatically listens for events from this interface. Public event propertychangedeventhandler propertychanged; protected void firepropertychanged (string propertyname) {If (propertychanged! = NULL) propertychanged (this, new propertychangedeventargs (propertyname);} Then the set accessors In the attribute reuse set {If (_ age! = Value) {_ age = value; firepropertychanged ("Age") ;}} 3. Dependency attributes: the values of these attributes can be dependent on the attribute values of other objects through binding, driven by the attribute values of other objects. 4. Bind path: the value to be bound (propertypath type) bind. path = new propertypath ("name"); // you can set the Data Binding path to text = {binding Path = text. length, elementname = textbox1, mode = oneway} 5. binding mode: onetime) twoway (two-way binding) 6. Source A of the data source, single object of the common CLR type: Custom class or. net built-in type; B. Set Type: array, list <t>, observablecollection <t>, and other C and ADO. net Data Objects: datatable and dataview (not supported by Win8 and WP) D. Use xmldataprovider to use XML as the data source (like win 8 not supported) E. The dependent object (which can be used as the source and can be used as the binding target) F. Specify the container's datacontext as the source: We know which attribute to obtain data from, but I don't know which object to use as the binding source. G. elementname as the Data source: bind h between elements and use the relativesource attribute of the binding class: Internal binding of the control (supporting itself but not supporting hierarchical binding) I. The objectdataprovider object is specified as source: when the data of the data source is not exposed to the outside world through attributes. (It seems that Win8 does not support) K. Use the Data Object retrieved by using LINQ as the source. 7. converter ivalueconverter converter: conversion from source to target converterback: conversion from target to source 8. Data Validation validationgrule looks like Win8 and WP are not supported. (If anyone has studied this, I hope you can make a research together.) Chapter 7 Introduction to attributes 1. static fields have only one copy in the memory, and non-static fields have one copy for each instance, however, no matter whether the method is static or non-static, there will be only one copy in the memory. The difference is that you can only access the memory for storing commands through the class name or Instance name. 2. Dependency attributes: one way is to get the value from the data source through binding. Objects with dependency attributes are called dependent objects. This saves the memory overhead attribute values of instances and relies on other objects through binding, it can be used as a data source and can be used as a data binding destination. 3. memory usage of dependency properties: the object does not contain the space used to store data (that is, the space occupied by the field) only retain the default value and borrow it when you need to use the data. The ability to allocate space for object data or real-time. A. dependencyproperty must host dependencyobject and set and obtain values through its setvalue and getvalue, b. The declaration must start with public static readonly. C. Property must be added after the name of the property member variable to indicate that it is a Dependency Property D and dependencyproperty. register (string name, type propertytype, type ownertype, propertymetadata typemetadata) Create instead of new, the first parameter is used to specify which CLR attribute is used as the package for this dependency attribute (the package exposes the dependency Attribute Based on the instance attribute ). The second parameter is used to specify the type of value stored in the dependency attribute. The third parameter is used to specify the type of the host of the dependency attribute.

// Step 1: Inherit dependencyobject class studentdependency: dependencyobject {// Step 3: Use the CLR attribute as the dependency attribute's wrapper public string name {get {return (string) getvalue (nameproperty) ;}set {setvalue (nameproperty, value) ;}// Step 2: register the dependency attribute public static readonly dependencyproperty nameproperty = dependencyproperty. register ("name", typeof (string), typeof (studentdependency), new propertymetadata (null);} 4. Additional property: an attribute is not an object. But it is attached due to a certain requirement, that is, it has the attributes after the object is put into a specific environment. This function decouples attributes from the data type (host) to make the data type design more flexible, the two are only slightly different in the wrapper and registration. // the two methods are used to assign a value to the attribute public static int getgrade (dependencyobject OBJ) {return (INT) obj. getvalue (gradeproperty);} public static void setgrade (dependencyobject OBJ, int value) {obj. setvalue (gradeproperty, value);} // when used, the registerattached method registers public static readonly dependencyproperty gradeproperty = dependencyproperty. registerattached ("Grade", typeof (INT), typeof (schoolattachedproperty), new propertymetadata (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.