在.NET 4.0 WPF中,如何通过左鼠标按钮检测到双击?
看似微不足道的任务。
我没有看到使用MouseDoubleClick
确定System.Windows.Input.MouseButtonEventArgs
事件中按下了哪个按钮的方法。
答案 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;
}
}
}