#1,088 – Removing Key Bindings

You’ll sometimes discover that there are built-in key bindings that you want to get rid of.  If you define the KeyBinding shown below, you’ll discover that the Ctrl+Alt+O key sequence works to execute the Open command, as expected.  But the Ctrl+O sequence also appears to do the same thing.

    <Window.CommandBindings>
        <CommandBinding Command="ApplicationCommands.Open"
                        Executed="Executed_Open"
                        CanExecute="CanExecute_Open"/>
    </Window.CommandBindings>
    <Window.InputBindings>
        <KeyBinding Command="ApplicationCommands.Open"
                    Gesture="Ctrl+Alt+O"/>
    </Window.InputBindings>

Ctrl+O is automatically defined as an existing key binding that binds to the ApplicationCommands.Open command.  You can remove it by binding Ctrl+O to the ApplicationCommands.NotACommand command.  Ctrl+O will no longer be associated with the Open command.

        <Window.InputBindings>
            <KeyBinding Command="ApplicationCommands.Open"
                        Gesture="Ctrl+Alt+O"/>
            <KeyBinding Command="ApplicationCommands.NotACommand"
                        Gesture="Ctrl+O"/>
        </Window.InputBindings>
Advertisements