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