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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11868956/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 04:54:10  来源:igfitidea点击:

How do you detect a mouse double left click in WPF?

.netwpf

提问by AlexPi

In .NET 4.0 WPF, how do you detect a double-click by the leftmouse button?

在.NET 4.0中WPF,你怎么检测通过双击左侧的鼠标按钮?

A seemingly trivial task.

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

I don't see a way of determining which button was pressed in the MouseDoubleClickevent using the System.Windows.Input.MouseButtonEventArgs.

我没有看到MouseDoubleClick使用System.Windows.Input.MouseButtonEventArgs.

回答by Reed Copsey

MouseDoubleClickpasses MouseButtonEventArgsas the event arguments. This exposes the ChangedButtonproperty, which tells you which button was double clicked.

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

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

回答by N_A

Are you looking for MouseButtonEventArgs.ChangedButton? API here.

你在找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;
        }
    }
}