ScottG
ScottG

Reputation: 11111

How can I handle the mouse wheel click event in WPF?

I want to close a tab in my tab control when the mouse wheel is clicked. How can I capture this event in WPF?

EDIT: Here's the code:

private void tabMain_MouseDown(object sender, MouseButtonEventArgs e)
    {
        if(e.ChangedButton == MouseButton.Middle && e.ButtonState == MouseButtonState.Pressed)
        {
            MessageBox.Show("Middle button clicked");
        }
    }

Upvotes: 15

Views: 17028

Answers (3)

In case someone is trying to catch this event but fails, check if sender has mouse gestures "intercepting" and handling it before it reaches MouseDown event.

Here is my situation with HelixViewport3D from HelixTookit library. MouseWheelDown is caught in MouseDown event only after i nullify following gesture:

myHelixPort.PanGesture2 = new MouseGesture(MouseAction.None);

Hope it'll save you some time.

Upvotes: 0

Rob
Rob

Reputation: 3863

An even easier solution

if (e.MiddleButton) { MessageBox.Show("Middle button clicked"); }

Upvotes: 3

Jobi Joy
Jobi Joy

Reputation: 50048

Mousewheel is actually the MiddleButton, So the condition for Wheel click on a MouseDown event is ChangedButton == Middle && ButtonState == Pressed

Upvotes: 19

Related Questions