2010-11-24 121 views

回答

1

好吧,我做了這樣的事情在我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; 
    } 
}; 

我認爲行爲方法可以讓事情變得更清潔,更可重複使用的,但我並沒有真正得到它。如果有人在這裏以簡單的方式解釋它會很好嗎?

4

它可以使用非常簡單的定製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="..." /> 
0

我簡單綁定使用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鍵關閉時,應用程序實際上會進行縮放。