有沒有一種方法可以將命令綁定到Ctrl+MWheelUp/Down
?你知道在瀏覽器中,你可以做同樣的增加/減少字體大小?我想在WPF中複製該效果。可能?我在看InputBinding>MouseBindings和MouseAction似乎不支持鼠標滾動。WPF InputBinding Ctrl + MWheelUp/Down可能嗎?
*我似乎已經張貼了類似的問題,但無法找到它了
有沒有一種方法可以將命令綁定到Ctrl+MWheelUp/Down
?你知道在瀏覽器中,你可以做同樣的增加/減少字體大小?我想在WPF中複製該效果。可能?我在看InputBinding>MouseBindings和MouseAction似乎不支持鼠標滾動。WPF InputBinding Ctrl + MWheelUp/Down可能嗎?
*我似乎已經張貼了類似的問題,但無法找到它了
好吧,我做了這樣的事情在我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;
}
};
我認爲行爲方法可以讓事情變得更清潔,更可重複使用的,但我並沒有真正得到它。如果有人在這裏以簡單的方式解釋它會很好嗎?
窗口具有MouseWheel事件。您可以執行一些命令綁定魔法,然後您可以綁定到DataContext屬性。看看這篇文章的提示:Key press inside of textbox MVVM。也看看這篇文章:http://code.msdn.microsoft.com/eventbehaviourfactor
它可以使用非常簡單的定製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="..." />
我簡單綁定使用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鍵關閉時,應用程序實際上會進行縮放。