[ACCEPTED]-WPF: Button single click + double click issue-mouseclick-event

Accepted answer
Score: 16

If you set the RoutedEvent's e.Handled to true after handling the 7 MouseDoubleClick event then it will not call the Click Event 6 the second time after the MouseDoubleClick.

There's a recent post which 5 touches on having different behaviors for 4 SingleClick and DoubleClick which may be useful.

However, if you 3 are sure you want separate behaviors and 2 want/need to block the first Click as well as 1 the second Click, you can use the DispatcherTimer like you were.

private static DispatcherTimer myClickWaitTimer = 
    new DispatcherTimer(
        new TimeSpan(0, 0, 0, 1), 
        DispatcherPriority.Background, 
        mouseWaitTimer_Tick, 
        Dispatcher.CurrentDispatcher);

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");
}
Score: 7

You could try this:

Button.MouseLeftButtonDown += Button_MouseLeftButtonDown;

private void Button_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
    e.Handled = true;

    if (e.ClickCount > 1)
    {
        // Do double-click code
    }

    else
    {
        // Do single-click code
    }
}

If neccessary, you could 2 require mouse click and wait until mouse 1 up to perform the action.

More Related questions