使用触发器在鼠标悬停时设置文本块前景 [英] using a trigger to set a textblock foreground on mouseover
问题描述
我正在尝试设置块,以便它的前景色每次都会改变鼠标光标越过它,这是我的代码:
I am trying to set block so its foreground color will change every time the mouse cursor goes over it, and this is my code:
<TextBlock Foreground="blue" Margin="18,234,5,-2" Grid.RowSpan="3">
<Underline>Remove Message</Underline>
<TextBlock.Style>
<Style TargetType="TextBlock">
<Style.Triggers>
<Trigger Property ="IsMouseOver" Value="True">
<Setter Property= "Foreground" Value="Red"/>
</Trigger>
</Style.Triggers>
</Style>
</TextBlock.Style>
</TextBlock>
当我尝试将 setter 属性更改为任何其他属性时,例如 FontSize="30"
,该事件确实发生了.
When I try to change the setter property to any other, for example FontSize="30"
, the event does occur.
推荐答案
那是因为在控件上设置的属性会覆盖在样式中定义的属性,因此您的 Foreground="blue" 将覆盖您在样式中设置的任何内容.要解决此问题,您可以移动样式中的 Foreground="blue" 并将其从控件的属性中删除.
That is because the properties set on a control override the one defined in the Style, so your Foreground="blue" will override whatever you set in the style. To fix this, you can move the Foreground="blue" in the style and remove it from the properties of the control.
<TextBlock Margin="18,234,5,-2" Grid.RowSpan="3">
<Underline>Remove Message</Underline>
<TextBlock.Style>
<Style TargetType="TextBlock">
<Setter Property= "Foreground" Value="Blue"/>
<Style.Triggers>
<Trigger Property ="IsMouseOver" Value="True">
<Setter Property= "Foreground" Value="Red"/>
</Trigger>
</Style.Triggers>
</Style>
</TextBlock.Style>
</TextBlock>
这篇关于使用触发器在鼠标悬停时设置文本块前景的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!