We have talked about binding many times before, and it has a notification function. How does this notification function come out? Why didn't we use this notification function in our previous CLR attributes?
That's because we now have two more concepts in SL: dependencyobject and dependencyproperty.
Dependencyproperty is actually used in dependencyobject.
The following describes the two items respectively.
1. dependencyobject
1. It is a class defined in SL and has dependencyproperty.
2. It is a data object to be bound.
3. All our interface elements are derived from dependencyobject.
To a UML diagram
1. dependencyproperty
• Parameters for registration-name of property-type of owner-propertymetadata • types of registration-simple property 'dependencyproperty. register '-- register the Dependency Property-attach property' dependencyproperty. registerattached '-- register the additional property-sample: canvas. top, grid. column • Principle of dependency Property-DependencyObject.Dictionary <propertyname, propertyvalue> • why use Dependency Property-binding, inherit, multiple value providers, Attachment
Why does dependencyproperty provide binding and inheritance .... Such a function.
The principle is as follows:
In fact, it is a dictionary in the dependencyobject. This dictionary contains a series of propertynames and propertyvalue pairs, that is
Let's say we use a name to get the corresponding value. In this way, the attributes and classes are decoupled. Therefore, when we perform some features on these attributes, we can operate on these attributes separately.
UseCodeTo implement the principles of dependencyobject and dependencyproperty (Note: Only imitation, dependencyproperty in Sl is more complex, but the principle is similar)
Customize a dependencyobject
Public Class Mydependencyobject { # Region Dependency Property principle code Private Dictionary < String , Object > Properties = New Dictionary < String , Object >(); Protected Object Getvalue (mydependencyproperty property ){ If (Property. ownertype = This . GetType ()){ Return This . Properties [property. Name];} Else { // Sample for inherit principle. Mydependencyobject parent = This . Getparentelement (property. ownertype ); Return Parent. getvalue (property );}} Protected Void Setvalue (mydependencyproperty property, Object Value ){ This . Properties [property. Name] = Value ;} # Endregion # Region Private methods Private Mydependencyobject getparentelement (type ){ // Todo: Get parent element here. Return Null ;} # Endregion }
Define a dependencyproperty
Public Class Mydependencyproperty { # Region Constructor Private Mydependencyproperty ( String Name, type valuetype, type ownertype, Object Defaultvalue ){ This . Name = Name; This . Valuetype = Valuetype; This . Ownertype = Ownertype; This . Defaultvalue = Defaultvalue ;} Public Static Mydependencyproperty register ( String Name, type valuetype, type ownertype, Object Defaultvalue) {mydependencyproperty instance = New Mydependencyproperty (name, valuetype, ownertype, defaultvalue ); Return Instance ;} # Endregion # Region CLR Properties Public String Name { Get ; Private Set ;} Public Type valuetype { Get ; Private Set ;} Public Type ownertype { Get ; Private Set ;} Public Object Defaultvalue { Get ; Private Set ;} # Endregion }
Example:
Public Class Myparentelement: mydependencyobject { Public String Text { Get { Return ( String ) Getvalue (textproperty );} Set {Setvalue (textproperty, value );}} Public Static Readonly Mydependencyproperty textproperty = Mydependencyproperty. Register ( " Text " , Typeof ( String ), Typeof (Myparentelement ), " I'm parent. " );} Public Class Mychildelement: mydependencyobject { Public String Text { Get { Return ( String ) Getvalue (textproperty );} Set {Setvalue (textproperty, value );}} Public Static Readonly Mydependencyproperty textproperty = Mydependencyproperty. Register ( " Text " , Typeof ( String ), Typeof (Myparentelement ), " I'm child. " );}