Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The <code>MouseBinding</code> does not support mouse up actions, only mouse down actions, so you simply cannot do what you want to do using a <code>MouseBinding</code>. The simplest alternative is a code-behind event handler for the <code>MouseRightButtonUp</code> event on the same element you would have added the <code>MouseBinding</code> as an <code>InputBinding</code> to. But I suspect you are avoiding the event handler approach for your own reasons, but you should clarify if that is your intention.</p> <p>The remaining option available to use is some form of attached behavior. There are many ways to do this but I'll use the fairly standard <code>System.Windows.Interactivity</code> from Blend behaviors. All you have to do is attach an event trigger for right mouse button up and invoke the close command. Everything you need to do this is in the SDK but unfortunately the feature to invoke a command called <code>InvokeCommandAction</code> doesn't properly support routed commands so I've written an alternative called <code>ExecuteCommand</code>.</p> <p>Here is some sample markup:</p> <pre><code>&lt;Grid Background="White"&gt; &lt;Grid.CommandBindings&gt; &lt;CommandBinding Command="Close" Executed="CommandBinding_Executed"/&gt; &lt;/Grid.CommandBindings&gt; &lt;!--&lt;Grid.InputBindings&gt; &lt;MouseBinding Command="Close" MouseAction="RightClick"/&gt; &lt;/Grid.InputBindings&gt;--&gt; &lt;i:Interaction.Triggers&gt; &lt;i:EventTrigger EventName="MouseRightButtonUp"&gt; &lt;utils:ExecuteCommand Command="Close"/&gt; &lt;/i:EventTrigger&gt; &lt;/i:Interaction.Triggers&gt; &lt;StackPanel&gt; &lt;TextBox Text="Some Text"/&gt; &lt;/StackPanel&gt; &lt;/Grid&gt; </code></pre> <p>Your old method is commented out and the new method is below it.</p> <p>Here is the code-behind just to hook up the routed command:</p> <pre><code> private void CommandBinding_Executed(object sender, ExecutedRoutedEventArgs e) { Close(); } </code></pre> <p>Finally, here is the implementation of <code>ExecuteCommand</code>:</p> <pre><code>public class ExecuteCommand : TriggerAction&lt;DependencyObject&gt; { 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); } } } </code></pre> <p>If you are not using routed commands but are using say, an MVVM RelayCommand, you can don't need <code>ExecuteCommand</code> and you can use <code>InvokeCommandAction</code> instead. </p> <p>This example uses behaviors. If you are not familiar with behaviors, install the Expression Blend 4 SDK and add this namespace:</p> <pre><code>xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity" </code></pre> <p>and add <code>System.Windows.Interactivity</code> to your project.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload