Search code examples
c#wpfdatagridcommandkey-bindings

WPF - DataGrid shall not handle Ctrl+A


In my WPF application I have a custom key binding to one of my commands:

<KeyBinding Modifiers="Control" Key="A" Command="local:MainWindow.SelectAll" />

(What it does is it select the whole area on an image with a bounding box for later processing.)

There is also a DataGrid in the main window. The Ctrl+A key works well on the application until I once clicked into the DataGrid. From that point, the DataGrid handles it (but doesn't do anything since it is not a multiselect grid).

How can I achieve the the DataGrid doesn't handle Ctrl+A, so it will always fire my command?

Here's my DataGrid if that helps:

<DataGrid Name="myDataGrid" ItemsSource="{Binding}" SelectionMode="Single"
EnableRowVirtualization="True" SelectedCellsChanged="myDataGrid_SelectedCellsChanged"
IsReadOnly="True" />

Solution

  • You can remove this kind of included binding by using this:

    <DataGrid>  
    <DataGrid.InputBindings>
    <KeyBinding Gesture="Ctrl+A" Command="ApplicationCommands.NotACommand"/>  
    </DataGrid.InputBindings>  
    </DataGrid>