Iii. Dependency attributes

Source: Internet
Author: User

  1. Define dependency attributes
     Public   Class  Frameworkelement: uielement {  Public   Static   Readonly  Dependencyproperty marginproperty;  Static  Frameworkelement () {frameworkpropertymetadata metadata = New Frameworkpropertymetadata (New  Thickness (), frameworkpropertymetadataoptions. None); marginproperty = Dependencyproperty. Register ( "  Margin  " , Typeof (Thickness ), Typeof (Frameworkelement), metadata, Null  );}  Public  Thickness margin {  Set  {Setvalue (marginproperty, value );} Get { Return  (Thickness) getvalue (marginproperty );}}} 

    You can only add dependency attributes for dependent objects (Classes inherited from dependencyobject. It isDependencypropertyClass, attribute information should always be available, and may even need to be shared among multiple classes. Therefore, the dependencyproperty object must be defined as a static field of the associated class. According to the Conventions, the field name defining the dependency attribute is added with the word "property" at the end of the common attribute, and the readonly keyword is used for modification.
    Dependencyproperty cannot be directly instantiated because dependencyproperty does not have a public constructor and can only be created using the static dependencyproperty. Register () method.
    Two steps are required to register a dependency attribute. First, a frameworkpropertymetadata object is created. What services does the object want to use through the dependency attribute. Second, you can call the dependencyproperty. Register () Static Method to register attributes. In this step, you need to provide the following elements:
    1) attribute name: In this example, It is margin.
    2) data type used by the property: the thickness structure is used in this example.
    3) type with this attribute: In this example, it is the frameworkelement class.
    4) A frameworkpropertymetadata object with additional property settings. This element is optional.
    5) a callback function used to verify attributes. This element is optional.
    All the attributes of the frameworkpropertymetadata class are described as follows:


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.