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'm looking for an add-in to VS2008 that can enable structural highlighting ( vertical
I can't get my GridView to enable a user to sort a column of
Is there a framework that can be used to enable a C# Windows Service
How can I programmatically lock/unlock, or otherwise prevent/enable editing, a source file on Linux
How do I enable external access to MySQL Server? I can connect locally but
I need to programmatically enable READ COMMITTED SNAPSHOT in SQL Server. How can I
I'm using a logging module that can have reporting enabled/disabled at runtime. Calls generally
I've been using DJGPP for the first time recently and can't seem to enable
I want to enable auto update script in flex3. i am using below code
I'm using boost::function to enable the passing of a function to a Button constructor

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.