DataGrid loses focus when you click Delete

I am doing MVVM where the DataGrid is bound to an ObservableCollection using DeleteItemCommand connected to the DataGrid.InputBindings as follows:

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

The item and row are deleted when the user presses the delete key, but the grid loses focus. You have to click or drag to the grid to restore focus before hitting Delete to delete another row (quite an exciting annoyance). I tried setting DataGrid.CanUserDeleteRows = "False", but that does not make any difference.

I replaced the DataGrid with a ListView, and the ListView maintains focus.

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

+4
source share
3 answers

I solved this using the built-in functions of WPF DataGrid. The grid handles deleting elements by default if the base collection is editable (if the collection is for this purpose, no problem, otherwise you can add an intermediate collection ...). I avoided any key bindings and simply created a grid as follows:
<DataGrid ItemsSource="{Binding InvoiceItems}" IsReadOnly="False" CanUserDeleteRows="True" CanUserAddRows="False">
The ItemsSource collection is of type BidningCollection <>

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

And implement it as follows:

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

This is because you are likely to have at least two ways to remove an item from your base collection (keyboard with Del key, button) and maybe some other things to take care of when the item is deleted.

+3
source

I came across this a while ago. Somehow this event never rose. Try this approach .

In short, the PreviewKeyDown event PreviewKeyDown take you wherever you want.

And in an MVVM-friendly way:

 <i:Interaction.Triggers> <i:EventTrigger EventName="PreviewKeyDown"> <i:InvokeCommandAction Command="{Binding DeleteItemCommand}"/> </i:EventTrigger> </i:Interaction.Triggers> 
0
source

Have you checked this answer yet?

How to associate a delete action (in WPF Datagrid) with a command or property in a view model

You probably need:

  • make sure CanUserDeleteRows = "False"
  • make sure the key is actually bound to the specified command in your datacontext, for example:

     <DataGrid.InputBindings> <KeyBinding Key="Delete" Command="{Binding DataContext.DeleteEntry, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=DataGrid}}"/> </DataGrid.InputBindings> 

I used to fail on element # 2 and wrote Command = "{Binding DeleteEntry}", while in reality I have to bind to DataContext.DeleteEntry using RelativeSource.

0
source

Source: https://habr.com/ru/post/1392739/


All Articles