我们如何在 WPF 应用程序中分离单击和双击列表视图?

问题描述:

我有一个 WPF 应用程序.有一个列表视图,每次我单击或双击时,都会触发单击事件.即使我保留了 Click 事件,当我双击它时它也会自动启动.如果我在 DoubleClick 中绑定操作,它在单击时将不起作用.

I have a WPF application. There is a listview in which a every time I click or double click, the click event fires up. Even if I keep the Click Event, it automatically fires up when I Double click it. And if I bind the action in DoubleClick, it won't work in single click.

我如何分别处理两者?

根据定义,双击的第二次单击总是在单击之前.

The second click of a double-click is by definition always preceded by a single click.

如果您不想处理它,您可以使用计时器等待大约 200 毫秒,以查看在您实际处理事件之前是否还有另一次点击:

If you don't want to handle it you could use a timer to wait for like 200 ms to see if there is another click before you actually handle the event:

public partial class MainWindow : Window
{
    System.Windows.Threading.DispatcherTimer _timer = new System.Windows.Threading.DispatcherTimer();
    public MainWindow()
    {
        InitializeComponent();
        _timer.Interval = TimeSpan.FromSeconds(0.2); //wait for the other click for 200ms
        _timer.Tick += _timer_Tick;
    }

    private void lv_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
    {
        if(e.ClickCount == 2)
        {
            _timer.Stop();
            System.Diagnostics.Debug.WriteLine("double click"); //handle the double click event here...
        }
        else
        {
            _timer.Start();
        }
    }

    private void _timer_Tick(object sender, EventArgs e)
    {
        System.Diagnostics.Debug.WriteLine("click"); //handle the Click event here...
        _timer.Stop();
    }
}

<ListView PreviewMouseLeftButtonDown="lv_PreviewMouseLeftButtonDown" ... />