Alan392
Alan392

Reputation: 695

Color Style Textblock

I have this textblock with default foreground color is white

 <TextBlock Text="First Cmd" Grid.Row="0" TextAlignment="Center"  Margin="4" TextWrapping="Wrap" Foreground="White" Style="{DynamicResource ABC}">
       <TextBlock.InputBindings>
                <MouseBinding  Command="{Binding AAA}" MouseAction="LeftClick" />
       </TextBlock.InputBindings>
 </TextBlock>

When the mouse is over the textblock, the forground color must change in black, but this Style doesn't work. Why ?

<Style x:Key="ABC" TargetType="{x:Type TextBlock}">
    <Style.Triggers>
        <Trigger Property ="IsMouseOver" Value="True">
            <Setter Property= "Foreground" Value="Black">
        </Trigger>
    </Style.Triggers>
</Style>

Upvotes: 3

Views: 3846

Answers (1)

King King
King King

Reputation: 63377

You set the Foreground for TextBlock locally, so the Trigger setter cannot override that. You need to use a Style setter to set the initial Foreground:

<Style x:Key="ABC" TargetType="{x:Type TextBlock}">
  <Setter Property="Foreground" Value="White"/>
  <Style.Triggers>
    <Trigger Property ="IsMouseOver" Value="True">
        <Setter Property= "Foreground" Value="Black">
    </Trigger>
  </Style.Triggers>
</Style>

The Foreground="White" should be removed from the <TextBlock ....

Understand more about Dependency Property Value Precedence.

Upvotes: 6

Related Questions