Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3980766
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T05:19:47+00:00 2026-05-20T05:19:47+00:00

How can I enable a mouse binding to the release of the right button?

  • 0

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" />
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T05:19:48+00:00Added an answer on May 20, 2026 at 5:19 am

    The MouseBinding does not support mouse up actions, only mouse down actions, so you simply cannot do what you want to do using a MouseBinding. The simplest alternative is a code-behind event handler for the MouseRightButtonUp event on the same element you would have added the MouseBinding as an InputBinding to. 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.Interactivity 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 InvokeCommandAction doesn’t properly support routed commands so I’ve written an alternative called ExecuteCommand.

    Here is some sample markup:

    <Grid Background="White">
        <Grid.CommandBindings>
            <CommandBinding Command="Close" Executed="CommandBinding_Executed"/>
        </Grid.CommandBindings>
        <!--<Grid.InputBindings>
            <MouseBinding Command="Close" MouseAction="RightClick"/>
        </Grid.InputBindings>-->
        <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.

    Here is the code-behind just to hook up 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 are not using routed commands but are using say, an MVVM RelayCommand, you can don’t need ExecuteCommand and you can use InvokeCommandAction instead.

    This example uses behaviors. If you are not familiar with behaviors, 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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a validator which I can enable or disable using the following javascript:
I would like to know the code which can enable me to get a
I am writing an Android application which can enable and disable the Network Data
I have a screen where you can enable/disable modules for my Android application. For
Need to enable scrolling into tmux panels with mouse wheel. How can I get
For example right now, when you click left mouse and choose paste (if enable)
I have seen an application named camera mouse which enables movement of cursor with
I see that you can enable/disable using the EnableWindow method, but how do I
Does anybody know how I can enable line numbers for Ruby (and Ruby on
plz guide me how I can enable or disable asp.net validation controls using jQuery

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.