Silverlight dependencyobject and dependencyproperty

Source: Internet
Author: User

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.  "  );} 

 

Related Article

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.