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

3

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");
        }
    }
Helenehelenka answered 5/2, 2009 at 19:36 Comment(0)
B
19

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

Burdette answered 5/2, 2009 at 19:42 Comment(1)
Is there a reason to check for ButtonState == ButtonState.Pressed in MouseDown event? Or it was just a copy/paste from somewhere?Selena
H
3

An even easier solution

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

Hacksaw answered 6/10, 2015 at 2:23 Comment(1)
in wpf? MouseButtonEventArgs.MiddleButton is an enum. cant convert to bool.Unclad
I
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.

Iseult answered 27/5, 2022 at 17:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.