Simulate the implementation of WPF dependency attributes and the binding notification mechanism (4)-simulate the implementation of the binding Linkage Mechanism

Source: Internet
Author: User

1. Example of a dependency object:

Public class mydendencycontrol: mydependencyobject
{
Public static readonly mydependencyproperty contentdependencyproperty =
Mydependencyproperty. Register ("content", typeof (string), typeof (mydendencycontrol), new mypropertymetadata ("hello "));

// Encapsulate the dependency attribute of a common attribute. Note that the method of the base class is called.
Public String content
{
Get
{
Return base. getvalue (contentdependencyproperty). tostring ();
}
Set
{
Base. setvalue (contentdependencyproperty, value );
}
}
}

2) a data provider class that implements the inotifypropertychanged Interface
Public class mypolicypropertyclass: inotifypropertychanged
{
Public event propertychangedeventhandler propertychanged;
Private void raisepropertychanged (string propertyname)
{
If (propertychanged! = NULL)
{
Propertychanged (this, new propertychangedeventargs (propertyname ));
}
}
Private string _ name;
Public string name
{
Get
{
Return _ name;
}
Set
{
If (_ name! = Value) // This is a good habit and can provide performance.
{
_ Name = value;
Raisepropertychanged ("name ");
}
}
}
}

3. Test linkage (Application)

// Create a dependent object instance
Mydendencycontrol thectrl = new mydendencycontrol ();
// Create a bound target class
Mypolicypropertyclass theclass = new mypolicypropertyclass ();
// Build binding. This is a manual binding method. It is set in XAML and will be interpreted as the following code:
Mybinding thebinding = new mybinding ();
Thebinding. targetobject = theclass;
Thebinding. propertyname = "name ";
Thectrl. setbinding (mydendencycontrol. contentdependencyproperty, thebinding );
// Default Value
MessageBox. Show (thectrl. content );
Theclass. Name = "Hello, you are good! ";
// View the current value after the association attribute changes
MessageBox. Show (thectrl. content );
// If the dependency attribute changes, the associated class attributes are also changed.
Thectrl. content = "Are you ready? ";
MessageBox. Show (theclass. Name );

At this point, Microsoft's WPF dependency attributes, binding and notification attributes, and interaction mechanisms have been completed. Of course, they are just a simple simulation. The implementation of Microsoft is much more complicated, but the principle is basically the same.

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.