WPF Keyboard Modifier on MouseBinding

2019-06-25 09:13发布

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!

4条回答
何必那么认真
2楼-- · 2019-06-25 09:46

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;
    ...
查看更多
看我几分像从前
3楼-- · 2019-06-25 09:54

Use it with keybinding too!

查看更多
▲ chillily
4楼-- · 2019-06-25 09:59

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

查看更多
混吃等死
5楼-- · 2019-06-25 10:07

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>
查看更多
登录 后发表回答