Home > View Post

Invoking a Command on a Double Click (or other 'Mouse Gesture')

I saw this on an internal alias recently thought it would make a useful post. Most of us are aware that many controls in WPF can invoke an ICommand as a result of their default action. For example, to invoke a Command on the Click of a button we can simply do this:

<Button Command="YourCommand" />

Easy. But what if I want to invoke a command on a Double Click? Also easy...

        <MouseBinding Gesture="LeftDoubleClick" Command="YourCommand" />

So now YourCommand will be invoked when the button is double clicked. What about a CTRL and left button click? Also easy:

<MouseBinding Gesture="CTRL+LeftClick" Command="YourCommand" />

Nice. You can read more about MouseBindings and take a look at KeyBindings whilst your at it.

Tags: WPF

Josh Post By Josh Twist
10:21 AM
01 Apr 2008

» Next Post: Xaml for Config
« Previous Post: Workaround for missing ElementName in Silverlight 2.0 Binding

Comments are closed for this post.

Posted by Andrew Hilton @ 06 Apr 2008 7:47 PM
What about a command that activates on mouse move and another that activates on mouse up? Examples: zoom rectangle, drag and drop.

Posted by Brian W @ 07 Dec 2011 10:28 PM
Thank you! Worked perfectly.

© 2005 - 2022 Josh Twist - All Rights Reserved.