自定义 MouseGesture类的派生类,代码实现如下所示:
/// <summary>
/// 鼠标滚轮动作
/// </summary>
public class MouseWheelGesture : MouseGesture
{
public WheelDirection Direction { get; set; }
//向下滚
public static MouseWheelGesture Down
{
get
{
return new MouseWheelGesture() { Direction = WheelDirection.Down };
}
}
//向上滚
public static MouseWheelGesture Up
{
get
{
return new MouseWheelGesture() { Direction = WheelDirection.Up };
}
}
public MouseWheelGesture() : base(MouseAction.WheelClick)
{
}
public MouseWheelGesture(ModifierKeys modifiers) : base(MouseAction.WheelClick, modifiers)
{
}
public override bool Matches(object targetElement, InputEventArgs inputEventArgs)
{
if (!base.Matches(targetElement, inputEventArgs)) return false;
if (!(inputEventArgs is MouseWheelEventArgs)) return false;
var args = (MouseWheelEventArgs)inputEventArgs;
switch (Direction)
{
case WheelDirection.None:
return args.Delta == 0;
case WheelDirection.Up:
return args.Delta > 0;
case WheelDirection.Down:
return args.Delta < 0;
default:
return false;
}
}
}
枚举滑轮的几种情况:
public enum WheelDirection
{
None,
Up,
Down
}
绑定UI界面如下所示:
<UserControl.InputBindings>
<MouseBinding Gesture="{x:Static commonControl:MouseWheelGesture.Down}" Command="{Binding SliderWheelCommand}" CommandParameter="{x:Static commonControl:WheelDirection.Down}"/>
<MouseBinding Gesture="{x:Static commonControl:MouseWheelGesture.Up}" Command="{Binding SliderWheelCommand}" CommandParameter="{x:Static commonControl:WheelDirection.Up}" />
</UserControl.InputBindings>
<Slider Grid.Column="1" Minimum="1" Maximum="{Binding TotalPages}" Value="{Binding CurrentPage}" TickPlacement="None" Orientation="Vertical">
</Slider>
正好使用到了,参考一下
https://blog.csdn.net/weixin_42100963/article/details/107558398