Does WPF have mouse wheel scrolling up and down event
Asked Answered
G

2

21

I checked msdn. For event related to mouse wheel, there is only one option -- UIElement.MouseWheel

What I want to do is listening to mouse wheel scrolling forward(up) and backward(down) event.

Note: Not clicking the middle wheel button.

Gewgaw answered 13/6, 2014 at 22:53 Comment(0)
P
42

No, there is just one event. When you look at the MouseWheelEventArgs class there is a property Delta. Delta is positive when the wheel is rotated away from the user and negative when the wheel is rotated toward the user.

Phosphoresce answered 13/6, 2014 at 22:57 Comment(0)
P
14

For event related to mouse wheel, there is only one option

No, there are others.

There is also the PreviewMouseWheel (which functions just like the MouseWheel event but is operates at a different point in the keyboard and mouse handling.).

The Preview also has a Delta property which gives the direction of the wheel spin.


Example

private void PreviewMouseWheel(object sender, MouseWheelEventArgs e)
{
    if (e.Delta > 0)
        DoActionUp();

    else if (e.Delta < 0)
        DoActionDown();
}
Peppermint answered 11/1, 2019 at 18:59 Comment(7)
Apart from suggesting the Preview version of the event (which you usually don't want to do unless there's a good reason), how is this any different from the accepted answer?Intension
@JoeWhite Its not a preview as in the next version of WPF... it means to peek (take a preview) at what is the next value and is a valid event to use in WPF.Dufrene
I didn't say anything about the next version of WPF. I asked why you're using the special-purpose PreviewMouseWheel event when the plain old MouseWheel event will work, and why you think it's better than the accepted answer.Intension
The answer given said there was only one event, which is patently false. I am providing an alternative, is it better depends on the situation. There are many ways to program a solution; this is one other way. Preview... has worked for me in as much as doing a peek on a stack or a queue.Dufrene
I like this answer because it gives the code as well. However, you don't need the PreviewMouseWheel. You can just use MouseWheel event.Tiffinytiffy
Apparently, if the control uses the wheel (for example control with a scroll), then the event MouseWheel is not called at all while the event PreviewMouseWheel is called.Burley
it's because of how routed events work in WPF. they use routing strategies, for instance tunneling (PreviewNNNEvent) or bubbling (NNNEvent): learn.microsoft.com/en-us/archive/msdn-magazine/2008/september/…Fistic

© 2022 - 2024 — McMap. All rights reserved.