WPF 是否有鼠标滚轮上下滚动事件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24214804/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 11:54:52  来源:igfitidea点击:

Does WPF have mouse wheel scrolling up and down event

c#wpfmousewheel

提问by dongx

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

我检查了msdn。对于鼠标滚轮相关的事件,只有一种选择——UIElement.MouseWheel

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

我想要做的是监听鼠标滚轮向前(向上)和向后(向下)滚动事件。

Note: Notclickingthe middle wheel button.

注意:不要单击中间的滚轮按钮。

回答by John Melville

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.

不,只有一个事件。当您查看 MouseWheelEventArgs 类时,有一个属性 Delta。当滚轮远离用户旋转时,Delta 为正值,当滚轮朝向用户旋转时为负值。

回答by ΩmegaMan

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 MouseWheelevent and the below code code can be used too).

还有PreviewMouseWheel它的功能就像MouseWheel事件一样,下面的代码也可以使用)。

The Preview, if needed, has a Deltaproperty too which gives the direction of the wheel.

如果需要,预览也有一个Delta属性,它给出了轮子的方向。

Example

例子

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

    else if (e.Delta < 0)
        DoActionDown();
}