问题
Possible Duplicate:
Time Delay on Trigger
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.
EDIT: 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# Code:
private void OnButtonClickDresses(object sender, RoutedEventArgs e)
{
//Code for delay
//Code which should run only if mouse on button after 5 seconds
}
回答1:
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.
See a small example here
来源:https://stackoverflow.com/questions/12908968/how-to-check-if-mouse-is-over-a-button-in-wpf