WPF InputBinding Ctrl + MWheelUp/Down可能吗?

Jie*_*eng 1 wpf inputbinding

有没有办法可以绑定命令Ctrl+MWheelUp/Down?你知道在浏览器中,你可以做同样的事情来增加/减少字体大小吗?我想在WPF中复制这种效果.可能?我在看InputBinding > MouseBindings,MouseAction似乎不支持Mouse Scrolls.

*我似乎发布了一个类似的问题,但已经找不到了

igo*_*shi 6

它可以使用非常简单的自定义MouseGesture来完成:

public enum MouseWheelDirection { Up, Down}

class MouseWheelGesture:MouseGesture
{
    public MouseWheelDirection Direction { get; set; }

    public MouseWheelGesture(ModifierKeys keys, MouseWheelDirection direction)
        : base(MouseAction.WheelClick, keys)
    {
        Direction = direction;
    }

    public override bool Matches(object targetElement, InputEventArgs inputEventArgs)
    {
        var args = inputEventArgs as MouseWheelEventArgs;
        if (args == null)
            return false;
        if (!base.Matches(targetElement, inputEventArgs))
            return false;
        if (Direction == MouseWheelDirection.Up && args.Delta > 0
            || Direction == MouseWheelDirection.Down && args.Delta < 0)
        {
            inputEventArgs.Handled = true;
            return true;
        }

        return false;
    }

}

public class MouseWheel : MarkupExtension
{
    public MouseWheelDirection Direction { get; set; }
    public ModifierKeys Keys { get; set; }

    public MouseWheel()
    {
        Keys = ModifierKeys.None;
        Direction = MouseWheelDirection.Down;
    }

    public override object ProvideValue(IServiceProvider serviceProvider)
    {
        return new MouseWheelGesture(Keys, Direction);
    }
}
Run Code Online (Sandbox Code Playgroud)

在xaml中:

<MouseBinding Gesture="{local:MouseWheel Direction=Down, Keys=Control}" Command="..." />
Run Code Online (Sandbox Code Playgroud)