WPF Keyboard Modifier on MouseBinding

爷,独闯天下 提交于 2019-12-10 17:47:17

问题


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!


回答1:


Use it with keybinding too!




回答2:


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>



回答3:


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;
    ...



回答4:


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



来源:https://stackoverflow.com/questions/1667707/wpf-keyboard-modifier-on-mousebinding

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!