我要双击时如何避免点击? [英] how to avoid click when I want double click?

查看:70
本文介绍了我要双击时如何避免点击?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在WPF中有一个应用程序和一个按钮.在此按钮中,我想要实现代码的click事件,但我希望在用鼠标双击时执行其他代码,而不是click事件的代码.

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.

问题在于click事件的代码始终执行,我不知道在进行doulbe click时是否有避免该click事件执行的方法.

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.

我遵循MVVM模式,并使用MVVM指示灯将事件转换为命令.

I am follow the MVVM pattern and I use MVVM light to convert the event into a command.

谢谢.

推荐答案

在处理MouseDoubleClick事件以阻止第二次点击事件被触发之后,将RoutedEvent的e.Handled设置为True.

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

这篇关于我要双击时如何避免点击?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆