in

DĂ© specialist in .NET trainingen en consultancy

Jo-wen Mei

Dependency Property value precedence

In WPF, there are a  lot of ways how the value of a dependency property can be affected.

Here's a list that describes the precedence by which the property system applies the effective value.

 

At first, it might seem a bit uninteresting because these are just the "inner workings".

But there is a less obvious implication that you need to be aware of, especially when working with styles/templates.

 

Check out this code:

<Window x:Class="WpfApplication1.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="300" Width="300">
    <Window.Resources>
        <Style TargetType="Button">
            <Style.Triggers>
                <Trigger Property="IsMouseOver" Value="True">
                    <Setter Property="Content" Value="mouseOver" />
                </Trigger>
                <Trigger Property="IsMouseOver" Value="False">
                    <Setter Property="Content" Value="mouseNotOver" />
                </Trigger>
            </Style.Triggers>
        </Style>
    </Window.Resources>
    <Grid>
        <Button Height="23" Margin="88,0,115,38" Name="button1" VerticalAlignment="Bottom">Button</Button>
    </Grid>
</Window>

 

.csharpcode, .csharpcode pre { font-size: small; color: black; font-family: consolas, "Courier New", courier, monospace; background-color: #ffffff; /*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt { background-color: #f4f4f4; width: 100%; margin: 0em; } .csharpcode .lnum { color: #606060; }

One of two triggers is always fired. Either the mouse is over the button or not.

So at startup, you might expect the Content to have the value "mouseNotOver", right?

... nothing happens though....

 

In the list, check out nr 3: Local Value.

Setting the Content property in xaml (which happens when you drag a button onto the design surface!) has a higher precedence than the triggers.

 

So keep in mind: either just type in the xaml manually, or use the designer and remove the property settings that are not required.

Published nov 29 2008, 01:01 by Jo-wen
Filed under:

Comments

No Comments