Welcome to MSDN Blogs Sign in | Join | Help

Locally set value takes precedence over Style value

If a DependencyProperty’s value is locally set, this value takes precedence over whatever value specified in Style. For example, the only button’s Background is LightGreen instead of LightBlue:

<Grid xmlns="http://schemas.microsoft.com/winfx/avalon/2005" >
    <Grid.Resources>
        <Style>
            <Button Background="LightBlue"/>
        </Style>
    </Grid.Resources>
    <Button Content="Demo" Background="LightGreen"/>
</Grid>

The first example is indeed very obvious. Now take a look at the next example, in which PropertyTrigger within Style is used. When you move mouse over the only button, what will be its Background?

<Grid xmlns="http://schemas.microsoft.com/winfx/avalon/2005" >
    <Grid.Resources>
        <Style>
            <Button />
            <Style.VisualTriggers>
                <PropertyTrigger Property="IsMouseOver" Value="True">
                    <Set PropertyPath="Background" Value="LightBlue"/>
                </PropertyTrigger>
            </Style.VisualTriggers>
        </Style>
    </Grid.Resources>
    <Button Content="Demo" Background="LightGreen"/>
</Grid>

Because locally set value takes precedence over style value, the Background remains LightGreen when IsMouseOver is true. 

To see the effect of PropertyTrigger, move Background="LightGreen" into Style definition.

(This posting is provided "AS IS" with no warranties, and confers no rights. Use of included script samples are subject to the terms specified at http://www.microsoft.com/info/cpyright.htm)

Published Friday, April 22, 2005 2:31 AM by zhanbos
Filed under:

Comments

Friday, April 22, 2005 2:42 AM by Zhanbo Sun [MS]

# Locally set value &amp;amp;amp;gt; Style Value

(Note: Whoever has a better tool to post code into this blog system please contact me. )If a DependencyProperty’s...

# Attorney Search &raquo; Locally set value takes precedence over Style value

Anonymous comments are disabled
 
Page view tracker