在 .NET 4.0 WPF 中,如何检测鼠标左键双击?
一个看似微不足道的任务。
我看不到MouseDoubleClick
使用System.Windows.Input.MouseButtonEventArgs
.
MouseDoubleClick
MouseButtonEventArgs
作为事件参数传递。这会公开ChangedButton属性,该属性告诉您双击了哪个按钮。
void OnMouseDoubleClick(Object sender, MouseButtonEventArgs e)
{
if (e.ChangedButton == MouseButton.Left)
{
// Left button was double clicked
}
}
你在找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;
}
}
}