2015-10-20 80 views
2

我有这个文本块默认前景色为白色颜色样式文本块

<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> 

当鼠标在文本块,于地面颜色要黑的变化,但这种风格不起作用。为什么?

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

回答

3

你为TextBlock设置Foreground在本地,所以Trigger二传手不能覆盖。你需要使用一个样式setter方法来设置初始前景:

<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> 

Foreground="White"应该从<TextBlock ...被删除。

了解有关Dependency Property Value Precedence的更多信息。

+1

好.. !!! ;-) 非常感谢你 !!! – Alan392