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

问题描述 投票:0回答:4

我有一个 WPF 应用程序。有一个列表视图,每次我单击或双击时,都会触发单击事件。即使我保留单击事件,当我双击它时它也会自动触发。如果我在 DoubleClick 中绑定该操作,则单击将无法工作。

如何分别处理两者?

c# wpf
4个回答
1
投票

将处理程序添加到您的控件中:

<SomeControl  MouseDown="MyMouseHandler">
...
</SomeControl>

处理程序代码:

private void MyMouseHandler(object sender, MouseButtonEventArgs e)
{
    if (e.ClickCount == 2)
    {
        //Handle here
    }
}

1
投票

双击中的第二次单击根据定义始终先于单击。

如果您不想处理它,您可以使用计时器等待 200 毫秒左右,看看是否有另一次点击,然后再实际处理该事件:

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" ... />

0
投票

请尝试以下代码片段:

     if (e.ChangedButton == MouseButton.Left && e.ClickCount == 2) {
        // your logic here
    }

有关详细信息,请尝试 MSDN 上的链接


0
投票
© www.soinside.com 2019 - 2024. All rights reserved.