6

在 .NET 4.0 WPF 中,如何检测鼠标左键双击?

一个看似微不足道的任务。

我看不到MouseDoubleClick使用System.Windows.Input.MouseButtonEventArgs.

4

2 回答 2

17

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

void OnMouseDoubleClick(Object sender, MouseButtonEventArgs e)
{
    if (e.ChangedButton == MouseButton.Left)
    {
        // Left button was double clicked
    }
}
于 2012-08-08T16:30:23.433 回答
3

你在找MouseButtonEventArgs.ChangedButton吗?API在这里

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;
        }
    }
}
于 2012-08-08T16:30:40.690 回答