Search code examples
wpfxamltriggerstextblock

TextBlock Text property can't be set via style trigger if non-empty - why?


The XAML below does not work (the text does not change when mousing over):

<Window.Resources>
    <Style TargetType="TextBlock">
        <Style.Triggers>
            <Trigger Property="IsMouseOver" Value="true">
                <Setter Property="Text" Value="hover"/>
            </Trigger>
        </Style.Triggers>
    </Style>
</Window.Resources>
<Grid>
    <TextBlock Text="original"/>
</Grid>

But, if the Text attribute is missing:

 <Grid>
    <TextBlock/>
</Grid>

The text does change on mouse over. Anybody knows the theory behind this?


Solution

  • It's a DependencyProperty precedence issue, when you actually set the property as in:

    <TextBlock Text="original"/>
    

    that takes precedence over the value set in the trigger.

    see

    http://msdn.microsoft.com/en-us/library/ms743230.aspx