WPF: local value and default value of the dependency attribute

Source: Internet
Author: User

Read this article first: current value, base value, and local value of the WPF dependency attribute)

The default value of dependency property is the default value parameter set for property metadata when the dependency property is registered. For example, frameworkpropertymetadata constructor. The default value is the lowest priority among the dependent attribute values!

The local value of the dependency attribute is set by the dependencyobject. setvalue method. It is obtained through dependencyobject. readlocalvalue. Dependencyobject. getvalue returns the current value, not the local value.

 

For example, the default value of this simple dependency attribute definition is 0.

Class dobj: dependencyobject

{

Public int myproperty

{

Get {return (INT) getvalue (mypropertyproperty );}

Set {setvalue (mypropertyproperty, value );}

}

Public static readonly dependencyproperty mypropertyproperty =

Dependencyproperty. Register ("myproperty", typeof (INT), typeof (dobj), new frameworkpropertymetadata (0 ));

}

 

If a dependency object is defined, the default value is returned for getvalue (the dependency attribute definition encapsulated by CLR attribute) because other higher-priority dependency attribute values are not set, the local Value Returns dependencyproperty. unsetvalue (because it is not set ).

VaR dobj = new dobj ();

MessageBox. Show (dobj. myproperty. tostring ());

MessageBox. Show (dobj. readlocalvalue (dobj. mypropertyproperty). tostring ());

 

The code above will first output: 0, and then output: dependencyproperty. unsetvalue

 

Once the local value of the dependency attribute is changed (through setvalue), the current value returned by getvalue will be a local value (because of the high priority). Although the default value still exists in theory, however, it is not returned because of its low priority. Therefore, both the local value and the current value are the values set by setvalue.

// Set the local value

Dobj. myproperty = 12;

MessageBox. Show (dobj. myproperty. tostring ());

MessageBox. Show (dobj. readlocalvalue (dobj. mypropertyproperty). tostring ());

 

The Code outputs two 12 values.

 

Depeendencyobject has another method: clearvalue. Used to clear the local value, so that the default value is returned by getvalue again.

VaR dobj = new dobj ();

// Set the local value

Dobj. myproperty = 12;

MessageBox. Show (dobj. myproperty. tostring ());

// Clear the local value

Dobj. clearvalue (dobj. mypropertyproperty );

MessageBox. Show (dobj. myproperty. tostring ());

Output: 12 and 0.

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.