how to avoid click when I want double click?

本秂侑毒 提交于 2019-12-10 21:53:02

问题


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.


回答1:


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



回答2:


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.




回答3:


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.




回答4:


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.



来源:https://stackoverflow.com/questions/18376012/how-to-avoid-click-when-i-want-double-click

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!