Simulate the implementation of WPF dependency attributes and bind the notification mechanism (2)-Prepare the dependency object

Source: Internet
Author: User

One of the main functions of dependency objects is to manage the attribute values of dependency properties. All classes that need dependency properties can be inherited from this, without the need to manage the dependency attribute values. The main methods of dependent objects are setvalue and getvalue, which are different from those of common properties. Of course, in actual applications, dependency properties generally do not directly use these two methods, instead, it is encapsulated as a common property method. In addition, for dependency attributes, we also provide a callback mechanism that can be intercepted by the dependent object when the attribute value changes. The purpose of this mechanism is to increase the control over the dependency attributes, however, you must note that this control is for classes rather than instances. Therefore, you need to pay attention to this feature during processing.

1) to provide this feature, we need to define a delegate for callback:

Public Delegate void mypropertychangedcallback (mydependencyobject D, mydependencypropertychangedeventargs E );

2) define a so-called metadata class, in fact, to record the true value, callback parameter class

/// <Summary>
/// Attribute metadata class, which is used to store the callback function when the default value and value of an attribute change.
/// In fact, when registering the dependency attribute, you can add the members of this class directly, but this method is not conducive to expansion,
/// Because you can inherit the mypropertymetadata class to increase the information, without changing the dependency
/// Attribute structure.
/// </Summary>
Public class mypropertymetadata
{
Public mypropertychangedcallback propertychangedcallback {Get; private set ;}
Public object defaultvalue {Get; private set ;}
Public mypropertymetadata (Object defaultvalue = NULL, mypropertychangedcallback callback = NULL)
{
This. propertychangedcallback = callback;
This. defaultvalue = defaultvalue;
}
}

3) The Parameter definition is as follows: (here I just made a small modification on the basis of Microsoft's related classes)

/// <Summary>
/// Parameters when attributes are changed
/// </Summary>
Public class mydependencypropertychangedeventargs
{

// Summary:
// Obtain the attribute value after modification.
//
// Return result:
// The attribute value after the change.
Public object newvalue {Get; private set ;}
//
// Summary:
// Obtain the attribute value before the change.
//
// Return result:
// The attribute value before the change.
Public object oldvalue {Get; private set ;}
//
// Summary:
// Obtain the identifier of the dependency attribute with a value change.
//
// Return result:
// The Identifier Field of the dependency attribute with a value change.
Public mydependencyproperty property {Get; private set ;}
Public mydependencypropertychangedeventargs (Object newvalue, object oldvalue, mydependencyproperty Property)
{
This. newvalue = newvalue;
This. oldvalue = oldvalue;
This. Property = property;
}

}

4) In addition to the above functions, we also need to provide simulation of the WPF binding mechanism. Therefore, we also provide management and processing of binding in the dependent object class. The following is the definition of binding, it is much simpler than Microsoft's:

/// <Summary>
/// Bind the setting class.
/// </Summary>
Public class mybinding
{
Public object targetobject {Get; set ;}
Public String propertyname {Get; set ;}
}

This class mainly provides records of the bound target object and attribute. I am simulating it here. I didn't use the path method, but the principle is similar to that of the attribute, except for simple syntax segmentation, form an attribute path.

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.