WPF输入绑定Ctrl+;MWheelUp/Down是否可能?
有没有办法将命令绑定到WPF输入绑定Ctrl+;MWheelUp/Down是否可能?,wpf,inputbinding,Wpf,Inputbinding,有没有办法将命令绑定到Ctrl+MWheelUp/Down?你知道在浏览器中,你可以用同样的方法增加/减少字体大小吗?我想在WPF中复制这种效果。可能的我正在查看>并且似乎不支持鼠标滚动 *我似乎已经发布了一个类似的问题,但再也找不到了窗口有鼠标滚轮事件。您可以执行一些命令绑定魔术,然后将其绑定到DataContext属性。查看这篇SO文章以获取提示:。还可以看看这篇文章:窗口有鼠标滚轮事件。您可以执行一些命令绑定魔术,然后将其绑定到DataContext属性。查看这篇SO文章以获取提示:。还可
Ctrl+MWheelUp/Down
?你知道在浏览器中,你可以用同样的方法增加/减少字体大小吗?我想在WPF中复制这种效果。可能的我正在查看>并且似乎不支持鼠标滚动
*我似乎已经发布了一个类似的问题,但再也找不到了窗口有鼠标滚轮事件。您可以执行一些命令绑定魔术,然后将其绑定到DataContext属性。查看这篇SO文章以获取提示:。还可以看看这篇文章:窗口有鼠标滚轮事件。您可以执行一些命令绑定魔术,然后将其绑定到DataContext属性。查看这篇SO文章以获取提示:。还可以看看这篇文章:好的,我在我的
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;
}
};
this.KeyDown+=(s,e)=>
{
_leftCtrlPressed=(e.Key==Key.LeftCtrl)?true:false;
};
this.MouseWheel+=(s,e)=>
{
如果(_leftCtrlPressed){
如果(e.Delta>0)
_vm.Options.FontSize+=1;
否则如果(e.Delta<0)
_vm.Options.FontSize-=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;
}
};
this.KeyDown+=(s,e)=>
{
_leftCtrlPressed=(e.Key==Key.LeftCtrl)?true:false;
};
this.MouseWheel+=(s,e)=>
{
如果(_leftCtrlPressed){
如果(e.Delta>0)
_vm.Options.FontSize+=1;
否则如果(e.Delta<0)
_vm.Options.FontSize-=1;
}
};
我认为行为方法会使事情更干净,更可重用,但我并没有真正理解它。如果有人在这里用一种简单的方式来解释,那就太好了。可以使用非常简单的自定义鼠标来完成:
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);
}
}
public enum mouseweeldirection{Up,Down}
类鼠标滚轮手势:MouseGesture
{
公共鼠标滚轮方向{get;set;}
公共鼠标滚轮手势(修改键、鼠标滚轮方向)
:base(鼠标移动。车轮单击,按键)
{
方向=方向;
}
公共覆盖布尔匹配(对象targetElement、InputEventArgs和InputEventArgs)
{
var args=作为MouseWheelEventArgs的inputEventArgs;
如果(args==null)
返回false;
如果(!base.Matches(targetElement,inputEventArgs))
返回false;
如果(方向==MouseWheelDirection.Up&&args.Delta>0
||方向==鼠标滚轮方向。向下(&A)参数。增量<0)
{
inputEventArgs.Handled=true;
返回true;
}
返回false;
}
}
公共类鼠标滚轮:MarkupExtension
{
公共鼠标滚轮方向{get;set;}
公共ModifierKeys{get;set;}
公共鼠标轮()
{
Keys=ModifierKeys.None;
方向=鼠标滚轮方向。向下;
}
公共覆盖对象ProviderValue(IServiceProvider服务提供程序)
{
返回新的鼠标滚轮手势(键、方向);
}
}
在xaml中:
<MouseBinding Gesture="{local:MouseWheel Direction=Down, Keys=Control}" Command="..." />
可以使用非常简单的自定义鼠标进行此操作:
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);
}
}
public enum mouseweeldirection{Up,Down}
类鼠标滚轮手势:MouseGesture
{
公共鼠标滚轮方向{get;set;}
公共鼠标滚轮手势(修改键、鼠标滚轮方向)
:base(鼠标移动。车轮单击,按键)
{
方向=方向;
}
公共覆盖布尔匹配(对象targetElement、InputEventArgs和InputEventArgs)
{
var args=作为MouseWheelEventArgs的inputEventArgs;
如果(args==null)
返回false;
如果(!base.Matches(targetElement,inputEventArgs))
返回false;
如果(方向==MouseWheelDirection.Up&&args.Delta>0
||方向==鼠标滚轮方向。向下(&A)参数。增量<0)
{
inputEventArgs.Handled=true;
返回true;
}
返回false;
}
}
公共类鼠标滚轮:MarkupExtension
{
公共鼠标滚轮方向{get;set;}
公共ModifierKeys{get;set;}
公共鼠标轮()
{
Keys=ModifierKeys.None;
方向=鼠标滚轮方向。向下;
}
公共覆盖对象ProviderValue(IServiceProvider服务提供程序)
{
返回新的鼠标滚轮手势(键、方向);
}
}
在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;
}
}
}
private void mouseweelCommandExecute(mouseweelEventArgs e)
{
if(Keyboard.IsKeyDown(Key.LeftCtrl)| | Keyboard.IsKeyDown(Key.RightCtrl))
{
如果(e.Delta>0)
{
如果(Properties.Settings.Default.ZoomLevel<4)
Properties.Settings.Default.ZoomLevel+=.1;
}
否则如果(e.Delta<0)
{
如果(Properties.Settings.Default.ZoomLevel>1)
Properties.Settings.Default.ZoomLevel-=.1;
}
}
}
如果Delta上升,鼠标向上滚动,下降,鼠标向下滚动。我在一个应用程序中使用它,其中滚动将发生在可滚动的内容中,但当按下任一Ctrl键时,应用程序实际上会缩放。我只是使用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;
}
}
}
private void mouseweelCommandExecute(mouseweelEventArgs e)
{
if(Keyboard.IsKeyDown(Key.LeftCtrl)| | Keyboard.IsKe