How can I enable a mouse binding to the release of the right button? At the moment I have the following code in xaml which is linked to closing the wpf window. The problem here is that because it reacts to the rampup of the click when closing the window it activates a context menu on the desktop.
<MouseBinding Command="Close" MouseAction="RightClick" />
The
MouseBindingdoes not support mouse up actions, only mouse down actions, so you simply cannot do what you want to do using aMouseBinding. The simplest alternative is a code-behind event handler for theMouseRightButtonUpevent on the same element you would have added theMouseBindingas anInputBindingto. But I suspect you are avoiding the event handler approach for your own reasons, but you should clarify if that is your intention.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
System.Windows.Interactivityfrom 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 calledInvokeCommandActiondoesn’t properly support routed commands so I’ve written an alternative calledExecuteCommand.Here is some sample markup:
Your old method is commented out and the new method is below it.
Here is the code-behind just to hook up the routed command:
Finally, here is the implementation of
ExecuteCommand:If you are not using routed commands but are using say, an MVVM RelayCommand, you can don’t need
ExecuteCommandand you can useInvokeCommandActioninstead.This example uses behaviors. If you are not familiar with behaviors, install the Expression Blend 4 SDK and add this namespace:
and add
System.Windows.Interactivityto your project.