我有这个文本块,默认前景色是白色
<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>
Run Code Online (Sandbox Code Playgroud)
当鼠标位于文本块上时,forground颜色必须以黑色更改,但此样式不起作用.为什么?
<Style x:Key="ABC" TargetType="{x:Type TextBlock}">
<Style.Triggers>
<Trigger Property ="IsMouseOver" Value="True">
<Setter Property= "Foreground" Value="Black">
</Trigger>
</Style.Triggers>
</Style>
Run Code Online (Sandbox Code Playgroud)
您在本地设置了Foregroundfor TextBlock,因此Triggersetter无法覆盖它.您需要使用样式设置器来设置初始前景:
<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>
Run Code Online (Sandbox Code Playgroud)
该Foreground="White"应予以除名<TextBlock ....
了解有关依赖属性值优先级的更多信息.