如何在XAML中为边框设置MouseOver事件/触发器? [英] How to set MouseOver event/trigger for border in XAML?
本文介绍了如何在XAML中为边框设置MouseOver事件/触发器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我希望鼠标悬停在边框上时边框变成绿色,然后当鼠标不再悬停在边框上时边框又变成蓝色。
I want the border to turn green when the mouse is over it and then to return to blue when the mouse is no longer over the border.
我尝试此操作时没有任何运气:
I attempted this without any luck:
<Border
Name="ClearButtonBorder"
Grid.Column="1"
CornerRadius="0,3,3,0"
Background="Blue">
<Border.Triggers>
<Trigger Property="Border.IsMouseOver" Value="True">
<Setter Property="Border.Background" Value="Green" />
</Trigger>
<Trigger Property="Border.IsMouseOver" Value="False">
<Setter Property="Border.Background" Value="Blue" />
</Trigger>
</Border.Triggers>
<TextBlock
HorizontalAlignment="Center"
VerticalAlignment="Center"
Text="X" />
</Border>
如何为MouseOver设置触发器或事件?
How can one set a trigger or events for MouseOver?
推荐答案
是的,这很令人困惑...
Yes, this is confusing...
根据这篇博客文章,看来这是WPF的遗漏。
According to this blog post, it looks like this is an omission from WPF.
要使其正常工作,您需要使用一种样式:
To make it work you need to use a style:
<Border Name="ClearButtonBorder" Grid.Column="1" CornerRadius="0,3,3,0">
<Border.Style>
<Style>
<Setter Property="Border.Background" Value="Blue"/>
<Style.Triggers>
<Trigger Property="Border.IsMouseOver" Value="True">
<Setter Property="Border.Background" Value="Green" />
</Trigger>
</Style.Triggers>
</Style>
</Border.Style>
<TextBlock HorizontalAlignment="Center" VerticalAlignment="Center" Text="X" />
</Border>
我想这个问题并不普遍,因为大多数人倾向于将这种事情排除在外样式,因此可以在多个控件上使用。
I guess this problem isn't that common as most people tend to factor out this sort of thing into a style, so it can be used on multiple controls.
这篇关于如何在XAML中为边框设置MouseOver事件/触发器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文