WPF InputBinding Ctrl + MWheelUp / Down可能吗?

有没有办法可以将命令绑定到
Ctrl+MWheelUp/Down
?你知道在浏览器中,你可以做同样的事情来增加/减少字体大小吗?我想在WPF中复制这种效果。可能?我在看InputBinding> MouseBindings,MouseAction似乎不支持Mouse Scrolls。 *我似乎发布了一个类似的问题,但已经找不到了     
已邀请:
它可以使用非常简单的自定义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);
    }
}
在xaml中:
<MouseBinding Gesture="{local:MouseWheel Direction=Down, Keys=Control}" Command="..." />
    
好吧,我在
ShellView : Window
做了类似的事情
this.KeyDown += (s, e) =>
{
    _leftCtrlPressed = (e.Key == Key.LeftCtrl) ? true : false;
};

this.MouseWheel += (s, e) =>
{
    if (_leftCtrlPressed) {
        if (e.Delta > 0)
            _vm.Options.FontSize += 1;
        else if (e.Delta < 0)
            _vm.Options.FontSize -= 1;
    }
};
我认为Behavior方法会让事情变得更清晰,更可重用,但我并没有真正理解它。如果有人在这里以简单的方式解释它会很棒吗?     
Window有MouseWheel事件。您可以执行一些命令绑定魔术,然后可以绑定到DataContext属性。查看这篇SO文章的提示:在文本框MVVM中按键。另外看看这篇文章:http://code.msdn.microsoft.com/eventbehaviourfactor     
我只是使用Interaction.Triggers绑定命令。 您需要在XAML中引用表达式interactivetivity命名空间。
 <i:Interaction.Triggers>
    <i:EventTrigger EventName="PreviewMouseWheel">
        <cmd:InvokeCommandAction Command="{Binding MouseWheelCommand}"/>
    </i:EventTrigger>
</i:Interaction.Triggers>
然后在相关的命令中。
 private void MouseWheelCommandExecute(MouseWheelEventArgs e)
    {
        if (Keyboard.IsKeyDown(Key.LeftCtrl) || Keyboard.IsKeyDown(Key.RightCtrl))
        {
            if (e.Delta > 0)
            {
                if (Properties.Settings.Default.ZoomLevel < 4)
                    Properties.Settings.Default.ZoomLevel += .1;
            }
            else if (e.Delta < 0)
            {
                if (Properties.Settings.Default.ZoomLevel > 1)
                    Properties.Settings.Default.ZoomLevel -= .1;
            }
        }

    }
如果Delta正在上升,则鼠标向上滚动,下降则向下滚动。我在一个应用程序中使用它,滚动将在滚动内容中发生,但当任一Ctrl键关闭时,应用程序实际上缩放。     

要回复问题请先登录注册