Skip Navigation LinksHome > 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...

<Button>
    <Button.InputBindings>
        <MouseBinding Gesture="LeftDoubleClick" Command="YourCommand" />
    </Button.InputBindings>
</Button>

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
01 Apr 2008

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

Comments:

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

Post a comment:

Name  

E-mail (never shared)

URL

Comments  

Captcha ImageRefresh Image
What's this?
Enter code above