Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

.net

wpf

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.

like image 888
AlexPi Avatar asked Aug 08 '12 16:08

AlexPi


People also ask

What happens if I double-click an item with my left mouse button?

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.

How many times do you click the left mouse when double-clicking?

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.

What is the difference between mouse double-click and right click?

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.

What is the difference between a click and a double-click?

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.


2 Answers

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
    }
}
like image 84
Reed Copsey Avatar answered Oct 16 '22 07:10

Reed Copsey


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;
        }
    }
}
like image 32
N_A Avatar answered Oct 16 '22 09:10

N_A