Command for Button, check if right clicked
-
Hi guys! Is there a chance to find out, whether a button was right or left clicked by using commands? What I would like to do is add to commands to a button, one for left click and one for right click. Thanks in advance!
You can add a MouseBinding(or MouseGesture). Something like this,
<MouseBinding Gesture="LeftClick"
Command="ApplicationCommands.Open" />MouseBinding needs the Mouse action and the routed command to be executed. You will have to add another MouseBinding for Right-click and also change the Command value to the routed command to be executed in your case. For more details, see here[^], here[^] and here[^]
-
Hi guys! Is there a chance to find out, whether a button was right or left clicked by using commands? What I would like to do is add to commands to a button, one for left click and one for right click. Thanks in advance!
I have done something like that before. I put it in a custom control since the basic button class does not seem to provide such functionality. Hmmmm... sounds like an article that should be written...
-
I have done something like that before. I put it in a custom control since the basic button class does not seem to provide such functionality. Hmmmm... sounds like an article that should be written...
-
You can do it with a MouseBinding. Did you see my suggestion[^] ?
I did not see your reply until after I posted mine (nor did I consider the possibility of putting input bindings on individual controls instead of the window). Since I already have a solution working, I think I will stick with it. Plus, I prefer to write this:
<TextBlock controls:DualCommandProvider.LeftClickCommand="Open"
controls:DualCommandProvider.RightClickCommand="Close" />instead of this:
<TextBlock>
<TextBlock.InputBindings>
<MouseBinding Command="Open" MouseAction="LeftClick" />
<MouseBinding Command="Close" MouseAction="RightClick" />
</TextBlock.InputBindings>
</TextBlock>Especially since where I am using this, I am putting left and right click actions on many controls and the extra lines would make the flow of the design harder to follow.
-
I did not see your reply until after I posted mine (nor did I consider the possibility of putting input bindings on individual controls instead of the window). Since I already have a solution working, I think I will stick with it. Plus, I prefer to write this:
<TextBlock controls:DualCommandProvider.LeftClickCommand="Open"
controls:DualCommandProvider.RightClickCommand="Close" />instead of this:
<TextBlock>
<TextBlock.InputBindings>
<MouseBinding Command="Open" MouseAction="LeftClick" />
<MouseBinding Command="Close" MouseAction="RightClick" />
</TextBlock.InputBindings>
</TextBlock>Especially since where I am using this, I am putting left and right click actions on many controls and the extra lines would make the flow of the design harder to follow.
I have a different opinion though. I feel separating it into a different section would make it easier to follow. It would demarcate the properties and bindings into separate group instead of all of them being together. It looks neat with just two entries but I would go crazy when the properties/bindings set through XAML would increase and make the node look ugly. This method has the obvious advantages of inherent implementation and access to all Mouse gestures in addition to the left and right click. But hey, as you feel comfortable. Cheers :)
-
You can add a MouseBinding(or MouseGesture). Something like this,
<MouseBinding Gesture="LeftClick"
Command="ApplicationCommands.Open" />MouseBinding needs the Mouse action and the routed command to be executed. You will have to add another MouseBinding for Right-click and also change the Command value to the routed command to be executed in your case. For more details, see here[^], here[^] and here[^]