4

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.

Álvaro García
  • 18,114
  • 30
  • 102
  • 193

4 Answers4

3

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.

Kestami
  • 2,045
  • 3
  • 32
  • 47
  • I have tried this way. The problem is that if I want that the event is fired, I need to use the PreviewMouseLeftDown event instad of MouseLeftDown, so I think if I use the preview envent this code does not work. If I use the MouseLeftDown event is not fired. – Álvaro García Aug 22 '13 at 10:20
  • 2
    This has the problem that the double click is detected, but first always detect the first click, so the code of the single click is alwys executed. Later if there are a second click, the code of the double click is executed. – Álvaro García Aug 23 '13 at 08:58
2

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");
}
Community
  • 1
  • 1
wolfovercats
  • 490
  • 5
  • 11
  • But this does not fire the first click event? because I think that the problems is that when I do the first click of the double click the click event is fire because the application does not know if I want double click or not. – Álvaro García Aug 22 '13 at 09:15
  • with this solution when I instantiate the dispatcher, the dispatcher is running so is execute when I instantiate my view model. Is there any way to instantiate the dispatcher in a stop state? – Álvaro García Aug 23 '13 at 08:10
  • if I create the dispatcher in the constructor of the view model instead of in the declaration in the variable class and inmediatly I stop the dispatcher is not executed in the creation of the view model. – Álvaro García Aug 23 '13 at 09:16
  • I have a problem. How the dispatcher is static, I only can use static variables, but I need to use other properties of the view models that are not static. If I set the dispatcher as no static, then the dispatcher is not stoped. – Álvaro García Aug 23 '13 at 09:48
2

Here is a nice explanation on how to distinguish between clicks and double clicks. And it gives you 2 example on how you can achieve that.

Cosmin Ionascu
  • 7,018
  • 5
  • 25
  • 42
0

I shared this solution, that is a mix between the two others solutions. I can't give the accepted solution to both, sorry.

If I use the PreviewMouseLeftButtonDown to control when is single click or double click works, instead of using two events (click and doubleclick).

So I solve the problem with this code:

This first method control the click with the mouse.

private void MouseLeftButtonDownCommand(MouseButtonEventArgs e)
        {

            if (e.ClickCount == 1)
            {
                _dtMouseClick.Start();
            }

            else if(e.ClickCount > 1)
            {
                _dtMouseClick.Stop();

                //the code of the double click
            }
        }

This method is the method that is linked to the DispatcherTimer, that is execute if is not stopped with the second click of the mouse.

private void MouseClick_Tick(object sender, System.EventArgs e)
        {
            _dtrMouseClick.Stop();

            //code of the single click                
        }

The dispatcherTimer is create in the constructor of the view model

_dtBotonBuscarMouseClick =
                new System.Windows.Threading.DispatcherTimer(
                new TimeSpan(0, 0, 0, 0, 250),
                System.Windows.Threading.DispatcherPriority.Background,
                MouseClick_Tick,
                System.Windows.Threading.Dispatcher.CurrentDispatcher);
            _dtMouseClick.Stop();

The interval is 250ms that is the interval that is the time that the user has to double click.

In this solution, I use the same way to stop the dispatcherTimer, the stop() method, but for some reason if I use the two events (click and mouseDoubleClick) the dispatcherTimer is not stopped in the double click and if I use the MouseLeftButtonDown event the solution works.

Álvaro García
  • 18,114
  • 30
  • 102
  • 193