如何在WPF中检测鼠标左键双击?

时间:2012-08-08 16:27:27

标签: .net wpf

在.NET 4.0 WPF中,如何通过鼠标按钮检测到双击?

看似微不足道的任务。

我没有看到使用MouseDoubleClick确定System.Windows.Input.MouseButtonEventArgs事件中按下了哪个按钮的方法。

2 个答案:

答案 0 :(得分:13)

MouseDoubleClick传递MouseButtonEventArgs作为事件参数。这会公开ChangedButton属性,告诉您双击了哪个按钮。

void OnMouseDoubleClick(Object sender, MouseButtonEventArgs e)
{
    if (e.ChangedButton == MouseButton.Left)
    {
        // Left button was double clicked
    }
}

答案 1 :(得分:3)

您在寻找MouseButtonEventArgs.ChangedButton吗? API here

private void MouseButtonDownHandler(object sender, MouseButtonEventArgs e)
{
    Control src = e.Source as Control;

    if (src != null)
    {
        switch (e.ChangedButton)
        {
            case MouseButton.Left:
                src.Background = Brushes.Green;
                break;
            case MouseButton.Middle:
                src.Background = Brushes.Red;
                break;
            case MouseButton.Right:
                src.Background = Brushes.Yellow;
                break;
            case MouseButton.XButton1:
                src.Background = Brushes.Brown;
                break;
            case MouseButton.XButton2:
                src.Background = Brushes.Purple;
                break;
            default:
                break;
        }
    }
}