2012-08-08 85 views
6

在.NET 4.0 WPF中,如何通過左側的鼠標按鈕檢測到雙擊?如何檢測WPF中的鼠標雙擊左鍵?

看似微不足道的任務。

我沒有看到使用System.Windows.Input.MouseButtonEventArgs來確定在MouseDoubleClick事件中按下哪個按鈕的方法。

回答

11

MouseDoubleClick通過MouseButtonEventArgs作爲事件參數。這暴露了ChangedButton屬性,它告訴你哪個按鈕被雙擊。

void OnMouseDoubleClick(Object sender, MouseButtonEventArgs e) 
{ 
    if (e.ChangedButton == MouseButton.Left) 
    { 
     // Left button was double clicked 
    } 
} 
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; 
     } 
    } 
} 
+0

是的。而已。謝謝。 – AlexPi 2012-08-08 16:32:47