WPF Keyboard Modifier on MouseBinding
Asked Answered
B

4

6

I'm working with the MVVM pattern in WPF (a bit new to both).

I'd like to set up an InputBinding on a CheckBox that corresponds to a Control + Click event, but do not see a Modifiers property on the MouseBinding element. This is what I'd like to achieve (fictitious code, obviously- Modifiers doesn't exist):

<CheckBox>
     <CheckBox.InputBindings>
           <MouseBinding MouseAction="LeftClick" 
                         Command="{Binding CheckboxControlClickCommand}"
                         Modifiers="Control" />
     </CheckBox.InputBindings>
</CheckBox>

Any ideas on how to accomplish this without using events?

Thanks!

Bac answered 3/11, 2009 at 14:36 Comment(0)
B
1

I ended up using Keyboard.Modifiers in the Execute() context of the ICommand, which seemed to work just fine.

if (Keyboard.Modifiers != ModifierKeys.Control) return;
    ...
Bac answered 24/11, 2009 at 16:49 Comment(0)
S
13

Use it with keybinding too!

Staid answered 14/1, 2010 at 15:58 Comment(0)
C
8

An old question but looks like the MouseBinding now provides a Gesture attribute just for this..

<CheckBox>
     <CheckBox.InputBindings>
           <MouseBinding Gesture="CTRL+LeftClick" 
                         Command="{Binding CheckboxControlClickCommand}"/>
     </CheckBox.InputBindings>
</CheckBox>
Chavey answered 1/6, 2017 at 7:52 Comment(0)
B
1

I ended up using Keyboard.Modifiers in the Execute() context of the ICommand, which seemed to work just fine.

if (Keyboard.Modifiers != ModifierKeys.Control) return;
    ...
Bac answered 24/11, 2009 at 16:49 Comment(0)
O
0

I think a behavior would do the trick. You can take a look at this link.

Orison answered 3/11, 2009 at 15:33 Comment(1)
I don't think you're off-base; I could have used a behavior but didn't know that I could check the Keyboard.Modifiers in the Execute() context of the ICommand, which is what I ended up doing.Bac

© 2022 - 2024 — McMap. All rights reserved.