Search code examples
wpfwpfdatagrid

DataGrid Looses Focus When Delete Key is Pressed


I'm doing MVVM where a DataGrid is bound to an ObservableCollection with a DeleteItemCommand hooked up to the DataGrid.InputBindings as follows:

  <DataGrid.InputBindings>
      <KeyBinding Key="Delete" Command="{Binding DeleteItemCommand}" />
  </DataGrid.InputBindings>

The item and row are removed when the user hits the delete key but the grid looses focus. You have to click or tab to the grid so it regains focus before hitting Delete to remove another row (pretty freaking annoying). I tried setting the DataGrid.CanUserDeleteRows="False" but it doesn't make any difference.

I replaced the DataGrid with a ListView and the ListView retains focus.

Is this a bug with the DataGrid or am I doing something wrong? Peace and love, peace and love!


Solution

  • I solved this by using the built in functionality of WPF DataGrid. The grid handles removing items by default if the underlying collection is editable (if the collection is dedicated to this purpose that's no problem, otherwise an intermediate collection can be added...). I avoided any key bindings and just set up the grid like this:
    <DataGrid ItemsSource="{Binding InvoiceItems}" IsReadOnly="False" CanUserDeleteRows="True" CanUserAddRows="False">
    The ItemsSource collection is of type BidningCollection<>

    In my ViewModel (my DataContext) I add a handler for CollectionChanged event:
    InvoiceItems.CollectionChanged += InvoiceItemsCollectionChanged;

    And implement it like this:

    private void InvoiceItemsCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
        {
            if (e.Action != NotifyCollectionChangedAction.Remove)
                return;
            foreach (var oldItem in e.OldItems)
            {
                //do any other processing necessary
            }
        }
    

    That's because you will probably be having at least two ways of removing an item from you underlying collection (keyboard with Del key, some button) and maybe some other things to take care of when an item is deleted.