发布时WPF RightClick MouseBinding?

如何启用鼠标绑定到右键的释放? 目前我在xaml中有以下代码,它与关闭wpf窗口有关。 这里的问题是,因为它在关闭窗口时对点击的加速作出反应,它激活桌面上的上下文菜单。

 

MouseBinding不支持鼠标按下操作,只支持鼠标按下操作,因此您无法使用MouseBinding执行您想要执行的MouseBinding 。 最简单的替代方法是将MouseBinding作为InputBinding添加到同一元素上的MouseRightButtonUp事件的代码隐藏事件处理程序。 但我怀疑你是出于自己的原因而避开事件处理程序的方法,但你应该澄清这是否是你的意图。

可以使用的其余选项是某种forms的附加行为。 有很多方法可以做到这一点,但我将使用Blend行为中相当标准的System.Windows.Interactivity 。 您所要做的就是为鼠标右键添加事件触发器并调用close命令。 您需要执行此操作的所有内容都在SDK中,但遗憾的是,调用名为InvokeCommandAction的命令的function不能正确支持路由命令,因此我编写了一个名为ExecuteCommand的替代方法。

这是一些示例标记:

               

您的旧方法已被注释掉,新方法位于其下方。

以下是连接路由命令的代码隐藏:

  private void CommandBinding_Executed(object sender, ExecutedRoutedEventArgs e) { Close(); } 

最后,这是ExecuteCommand的实现:

 public class ExecuteCommand : TriggerAction { public ICommand Command { get { return (ICommand)GetValue(CommandProperty); } set { SetValue(CommandProperty, value); } } public static readonly DependencyProperty CommandProperty = DependencyProperty.Register("Command", typeof(ICommand), typeof(ExecuteCommand), null); public object CommandParameter { get { return (object)GetValue(CommandParameterProperty); } set { SetValue(CommandParameterProperty, value); } } public static readonly DependencyProperty CommandParameterProperty = DependencyProperty.Register("CommandParameter", typeof(object), typeof(ExecuteCommand), null); public UIElement CommandTarget { get { return (UIElement)GetValue(CommandTargetProperty); } set { SetValue(CommandTargetProperty, value); } } public static readonly DependencyProperty CommandTargetProperty = DependencyProperty.Register("CommandTarget", typeof(UIElement), typeof(ExecuteCommand), null); protected override void Invoke(object parameter) { if (Command is RoutedCommand) { var routedCommand = Command as RoutedCommand; var commandTarget = CommandTarget ?? AssociatedObject as UIElement; if (routedCommand.CanExecute(CommandParameter, commandTarget)) routedCommand.Execute(CommandParameter, commandTarget); } else { if (Command.CanExecute(CommandParameter)) Command.Execute(CommandParameter); } } } 

如果您没有使用路由命令但使用的是MVVM RelayCommand,则可以不需要ExecuteCommand ,而是可以使用InvokeCommandAction

此示例使用行为。 如果您不熟悉行为,请安装Expression Blend 4 SDK并添加此命名空间:

 xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity" 

并将System.Windows.Interactivity添加到您的项目中。