I have an application in WPF and one button. In this buttun I want the click event that implement a code, but I want that when the do double click with the mouse, execute other code but not the code of the click event.
The problem is that the code of the click event is always executed and I don't know if there is a way to avoid the execution of the click event when I do doulbe click.
I am follow the MVVM pattern and I use MVVM light to convert the event into a command.
Thanks.
On the click event, you can check the amount of clicks in the EventArgs, for example;
private void RightClickDown(object sender, MouseButtonEventArgs e)
{
if (e.ClickCount == 1)
{
//do single click work here.
}
if (e.ClickCount == 2)
{
//do double click work.
}
}
This means you can differentiate between single and double clicks manually, if that's what you desire.
Set the RoutedEvent's e.Handled to True after handling the MouseDoubleClick event to block second click events of being fired.
If you want to block first click event behavior, you can use a timer:
private static DispatcherTimer myClickWaitTimer =
new DispatcherTimer(
new TimeSpan(0, 0, 0, 0, 250),
DispatcherPriority.Normal,
mouseWaitTimer_Tick,
Dispatcher.CurrentDispatcher) { IsEnabled = false };
private void Button_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
// Stop the timer from ticking.
myClickWaitTimer.Stop();
Trace.WriteLine("Double Click");
e.Handled = true;
}
private void Button_Click(object sender, RoutedEventArgs e)
{
myClickWaitTimer.Start();
}
private static void mouseWaitTimer_Tick(object sender, EventArgs e)
{
myClickWaitTimer.Stop();
// Handle Single Click Actions
Trace.WriteLine("Single Click");
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With