15

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");
        }
    }
Nick Craver
  • 623,446
  • 136
  • 1,297
  • 1,155
ScottG
  • 10,711
  • 25
  • 82
  • 111

3 Answers3

19

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

Jobi Joy
  • 49,102
  • 20
  • 108
  • 119
  • Is there a reason to check for `ButtonState == ButtonState.Pressed` in **`MouseDown`** event? Or it was just a copy/paste from somewhere? – Sinatr Aug 19 '14 at 08:21
3

An even easier solution

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

Rob
  • 3,488
  • 3
  • 32
  • 27
0

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.