如何检查鼠标是否在WPF中的按钮? [英] How to check if mouse is over a button in WPF?
问题描述
可能重复:
触发器上的延迟
我在WPF中使用了几个按钮用于显示来自各种文件夹的图像。所有按钮的单击
模式为 Hover
。对于某些按钮,在悬停之后,它被路由到一个函数 OnButtonClick
。我想要做的是,只有当鼠标超过 X
秒的按钮后,函数 OnButtonClick
。
I am using a few buttons in WPF for displaying images from various folders. The Click
mode for all the buttons is Hover
. For some button, after hovering, it is routed to a function OnButtonClick
. What I want to do is, ONLY after the mouse is over the button for X
seconds, the processing inside the function OnButtonClick
should be done.
编辑:
XAML代码:
XAML Code:
<Button Name="Dresses" Content="Dresses" Click="OnButtonClickDresses" Grid.Row="2" Height="34" Width="85" VerticalAlignment="Center" Grid.Column="5" FontFamily="Tahoma" FontSize="14" FontWeight="Bold" HorizontalAlignment="Center" Cursor="Hand" ClickMode="Hover">
<Button.Background>
<LinearGradientBrush>
<GradientStop Color="Yellow" Offset="0" />
<GradientStop Color="Green" Offset="1" />
</LinearGradientBrush>
</Button.Background>
</Button>
C#代码:
private void OnButtonClickDresses(object sender, RoutedEventArgs e)
{
//Code for delay
//Code which should run only if mouse on button after 5 seconds
}
推荐答案
在MouseOver事件上使用触发器。一旦鼠标在你的控制,启动一个计时器。如果它在鼠标仍在控件上方的时候消失,请模拟一次点击。
Use a trigger on the MouseOver event. Once the mouse is over your control, start a timer. If it elapses while the mouse is still over the control, simulate a click.
查看一个小例子这里
这篇关于如何检查鼠标是否在WPF中的按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!