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) {
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.
jl. source share