In .NET 4.0 WPF, how do you detect a double-click by the left mouse button?
A seemingly trivial task.
I don't see a way of determining which button was pressed in the MouseDoubleClick
event using the System.Windows.Input.MouseButtonEventArgs
.
In most cases, a double-click is with the left mouse button and is used to open or execute a file, folder, or software program. For example, programs and files in Microsoft Windows are opened by double-clicking the icon. To open your Internet browser, you would double-click the browser icon shortcut.
A double-click is the act of pressing a computer mouse button twice quickly without moving the mouse. Double-clicking allows two different actions to be associated with the same mouse button.
Double click is nothing but performing a Left mouse click twice. Right-click is performing a single Right mouse click. This directly interacts with an object.
Typically, a single click initiates a user interface action and a double-click extends the action. For example, one click usually selects an item, and a double-click edits the selected item.
MouseDoubleClick
passes MouseButtonEventArgs
as the event arguments. This exposes the ChangedButton property, which tells you which button was double clicked.
void OnMouseDoubleClick(Object sender, MouseButtonEventArgs e)
{
if (e.ChangedButton == MouseButton.Left)
{
// Left button was double clicked
}
}
Are you looking for 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;
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With