WPF Learning (iii)-dependency properties and attached properties

Source: Internet
Author: User
Tags comparison table

When I was learning WPF, I was looking at a book called "The Most understandable WPF". Full 20 pages are talking about dependency and attached properties, repeated to see a few times actually still do not understand, really depressed.

The example of WPF bindings in the previous article has actually used dependency properties.

    //as the target class being bound, must derive from DependencyObject//this defines the class to satisfy the type requirements of the first parameter of the SetBinding method//An additional dependency property is defined to satisfy the second parameter requirement of the SetBinding method//using the DependencyObject derivation method GetValue and SetValue, the control attribute is stored, taken     Public classUIClass:System.Windows.DependencyObject { Public stringMyText {Get{return(string) GetValue (Mytextproperty); }            Set{SetValue (mytextproperty, value);} }         Public Static ReadOnlySystem.Windows.DependencyProperty Mytextproperty =System.Windows.DependencyProperty.Register ("MyText",typeof(string),typeof(Uiclass)); }
examples of use of dependency properties

From here, I know that: to synchronize the property values of the display layer to the data layer, you must use a dependency property.

In the previous article, the implementation process of the binding method with WinForm is insufficient. WPF uses the technology of dependency properties to solve the problem of WinForm.

So how does a dependency property magically do this? Before, I said, WinForm implementation is difficult, but not equal to the realization, this proposition is this:

Known: an instance and its property name string, how to get this property value of this instance? Note that this requirement is not in the coding phase, but in the function implemented during the run phase.

Getobjectpropertyvalue (Object o, String propertyname)//Return property value

Don't use reflection!!! In fact, such a simple request, I actually can't do it ... It doesn't have to remind me that when I first approached programming, I always used an alternative approach to achieve some standard functionality. This time it is still an alternative way to solve the problem. Anyway, the requirement is to take a value by name, I set up a name + value of the table, so that the value of the deposit/Fetch property by name is realized. Well, there are also examples to brush the existence of a sense, the comparison table to add a, describe the example.

     Public classPropertycontainer {StaticList<instancenamevalue> LSTINV =NewList<instancenamevalue> ( );  Public voidRegister (intInstancestringPropertyName) {Lstinv.add (NewInstancenamevalue () {Instancehashcode = instance, PropertyName =PropertyName}); }         Public ObjectGetValue (ObjectOstringPropertyName) {            foreach(Instancenamevalue IteminchLSTINV) {                if(O.gethashcode () = = Item. Instancehashcode && PropertyName = =item. PropertyName) {returnitem.                Value; }            }            return NULL; }    }    //instance, attribute name, value    classInstancenamevalue { Public intInstancehashcode;  Public stringPropertyName;  Public ObjectValue; }
Property Container

     Public class Cheatclass    {        publicstring  cheatproperty        {            getreturn ( string  This " Whatevername "  ); }        }    }
hypocritical classes, false attributes

Calling the Propertycontainer.getvalue method, passing in the instance and property names, reading and writing the property values corresponding to this instance, seems to be working. In fact, this is a lie, I never really read/write the attribute value of this instance (if he really is the kind of traditional meaning of class and attribute instantiation), I have been using an alias to save the value I want to save in the attribute field. Even the class where this instance resides does not have this field at all.

When I first started to understand this, I was furious. With so many years of object-oriented, now the class is no longer required to open up storage space, the object is no longer the object, the class and the relationship between the class? Have it??!!! Also design software do what, also play what data structure. Arbitrarily define an empty class, run to it, want to add what attributes at will add.

Once there was a child, so design data table structure: Table name, field name, record number, field value, but also very complacent to show me that I designed a universal structure, I slap the past, trained people to the data structure learned to the dog! Now, Microsoft slapped back, so wayward structure,. NET also need to use, how to do it.

Anger, this is the day ...

WPF Learning (iii)-dependency properties and attached properties

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.