有没有办法可以将命令绑定到Ctrl+MWheelUp/Down
?你知道在浏览器中,你可以做同样的事情来增加/减少字体大小吗?我想在WPF中复制这种效果。可能?我在看InputBinding> MouseBindings和MouseAction似乎不支持鼠标滚动。
*我似乎发布了一个类似的问题,但找不到了
答案 0 :(得分: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);
}
}
xaml中的:
<MouseBinding Gesture="{local:MouseWheel Direction=Down, Keys=Control}" Command="..." />
答案 1 :(得分: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;
}
};
我认为行为方法会使事情更清晰,更可重复使用,但我并没有真正得到它。如果有人在这里以简单的方式解释它会很棒吗?
答案 2 :(得分:0)
Window有MouseWheel事件。您可以执行一些命令绑定魔术,然后可以绑定到DataContext属性。查看此SO文章以获取提示:Key press inside of textbox MVVM。另请查看这篇文章:http://code.msdn.microsoft.com/eventbehaviourfactor
答案 3 :(得分:0)
我只是使用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键关闭时,应用程序实际上会缩放。