Search code examples
c#wpfmenuitemkey-bindings

C# how to disable keybinding


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?


Solution

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