WPF: current value, base value, and local value of the dependency attribute)

Source: Internet
Author: User

The current value, base value, and local value of dependency property are three common words in msdn, these attributes are related to the priority settings of dependency attributes.

See the following table:

Here is the current value

1. Forced conversion of the attribute system. Here, the coercevaluecallback of the dependency attribute is used.

2. Activity animation or animation with hold Behavior

Here is the base value

3. Local value, through the setvalue method of the dependency attribute, resource, binding

4. other methods such as style and trigger ......

 

Here is a list of cases where the priority of a dependency property value is listed. For a complete list, see msdn: http://msdn.microsoft.com/zh-cn/library/ms743230.aspx.

 

Current value:

Is the final value of the outermost layer, which may be obtained after processing or influencing some columns.

Base Value:

When the Dependency Property is not forcibly converted by the system and the value is not affected by animation processing, it is the value above the third item in the table but not affected by the first and second items.

Local value:

This is a relatively intuitive set value, which is set through the setvalue method of the dependency attribute or resource or binding.

 

How can we get these values?

Current Value: The getvalue method of the dependency attribute

Base Value: The getanimationbasevalue method through the ianimatable Interface

Local value: the readlocalvalue method of the dependency attribute. If no local value exists, the dependencyproperty. unsetvalue field is returned.

 

Assume there are two buttons named btn1 and btn2 respectively. After the program runs, the width (width dependency attribute) of the button will be changed from 50 to 100 by the doubleanimation animation, the only difference between the two buttons is that the width of btn1 is directly set. The width of btn2 is set through the setter of the style.

When the animation ends, the width of the two buttons depends on the specific value of the attribute as follows:

  Btn1 Btn2
Current Value 100 100
Base Value 50 50
Local Value 50 Dependencyproperty. unsetvalue

 

Because the animation sets the dependency attribute, the current value is 100.

Before the animation, the width is 50, so the base value is 50.

Btn1 directly sets the width, that is, the setvalue of the dependency attribute is called, so the local value is set.

Btn2 uses the setter of the style to set the width, which is the fourth item in the preceding table. The local value is not set.

 

You can test the program as follows:

 

For more information about how to obtain attributes, see:

Object [] getvalues (Object OBJ, dependencyproperty Pro)

{

If (obj is dependencyobject = false | obj is ianimatable = false)

Throw new argumentexception ();

Object [] Re = new object [3];

 

VaR dobj = (dependencyobject) OBJ;

VaR ianm = (ianimatable) OBJ;

 

Re [0] = dobj. getvalue (Pro );

Re [1] = ianm. getanimationbasevalue (Pro );

Re [2] = dobj. readlocalvalue (Pro );

Return re;

 

}

 

String getvaluesstring (Object OBJ, dependencyproperty pro, string name)

{

VaR objs = getvalues (OBJ, Pro );

Return string. Format ("{3} \ n current value: {0} \ n Base Value: {1} \ n local value: {2} \ n ",

Objs [0], objs [1], objs [2], name );

}

 

Two buttons:

<Button width = "50" Height = "30" name = "btn1" content = "1">

<Button. triggers>

<Eventtrigger routedevent = "button. Loaded">

<Eventtrigger. Actions>

<Beginstoryboard>

<Storyboard>

<Doubleanimation storyboard. targetproperty = "width"

To = "100" Duration = ". 5"/>

</Storyboard>

</Beginstoryboard>

</Eventtrigger. Actions>

</Eventtrigger>

</Button. triggers>

</Button>

 

<Button Height = "30" name = "btn2" content = "2">

<Button. Style>

<Style targettype = "button">

<Setter property = "width" value = "50"/>

</Style>

</Button. Style>

<Button. triggers>

<Eventtrigger routedevent = "button. Loaded">

<Eventtrigger. Actions>

<Beginstoryboard>

<Storyboard>

<Doubleanimation storyboard. targetproperty = "width"

To = "100" Duration = ". 5"/>

</Storyboard>

</Beginstoryboard>

</Eventtrigger. Actions>

</Eventtrigger>

</Button. triggers>

</Button>

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.