【问题标题】:WPF InputBinding Ctrl+MWheelUp/Down Possible?WPF InputBinding Ctrl+MWheelUp/Down 可能吗?
【发布时间】:2010-11-24 03:32:31
【问题描述】:

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

* 我好像发过类似的问题,但是找不到了

【问题讨论】:

    标签: wpf inputbinding


    【解决方案1】:

    可以使用非常简单的自定义鼠标手势来完成:

    public enum MouseWheelDirection { Up, Down}
    
    public 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="..." />
    

    【讨论】:

      【解决方案2】:

      好的,我在 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 方法会让事情变得更清洁和更可重用,但我并没有真正理解。如果有人在这里以简单的方式解释它会很棒吗?

      【讨论】:

        【解决方案3】:

        Window 有 MouseWheel 事件。您可以执行一些命令绑定魔术,然后将其绑定到 DataContext 属性。查看这篇 SO 文章以获取提示:Key press inside of textbox MVVM。也可以看看这篇文章:http://code.msdn.microsoft.com/eventbehaviourfactor

        【讨论】:

          【解决方案4】:

          我只是使用 Interaction.Triggers 绑定命令。

          您需要在 XAML 中引用表达式交互命名空间。

           <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 键时,应用程序实际上会缩放。

          【讨论】:

          • 有关信息,cmd 前缀来自以下参考:跨度>
          猜你喜欢
          • 2015-10-07
          • 2016-06-26
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-04-24
          • 2014-09-16
          • 2010-09-30
          • 1970-01-01
          相关资源
          最近更新 更多