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.