WPF使用鼠标滚轮绑定命令

自定义 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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值