C# how to disable keybinding

2020-04-14 07:13发布

问题:

The MenuItem control has the convenient property IsEnabled (inherited from UIElement). This allows me to, when appropriate, hide the command exposed by that menu option.

But if that command is also bound to a key (e.g. Ctrl+K), the user can still access it. How do I get the IsEnabled functionality to a KeyBinding?

回答1:

If you bind the KeyBinding to an ICommand, you can just set the ICommand.CanExecute to false (and potentially raise CanExecuteChanged).

This will "disable" the KeyBinding since the command itself will be disabled.