maintain datagridcell focus on removing item

Apr 30, 2009 at 10:15 PM
I have a DataGrid bound to a CollectionViewSource. It's a special CLR object so it doesn't directly support deletion through the DataGrid. We actually intercept the delete key with PreviewKeyDown and issue our own CLR command to remove an item.

We have a problem though is that when this deletion occurs, the selected row stays selected and the collection decreases by one as we expect, but the keyboard focus jumps to select the whole DataGrid. 

I've tried setting focus at the end of the Delete function we used but still no avail. this is what we have. It uses Vinsibal's GetCell function.

private void DeleteAllSelectedCalls(object sender, RoutedEventArgs e)
         foreach (kudu.CallHistoryItem item in this.HistoryDataGrid.SelectedItems)
         DataGridCell cell = GetCell(HistoryDataGrid.SelectedIndex, 1);
The cell.Focus does get called, but then it somehow jumps to the DataGrid when the element is finally removed from the collection.

Any ideas?
May 12, 2009 at 8:37 PM

Turns out I was setting focus before my custom collection had actually deleted the selected element. I implemented a callback when the collection finally updated and focusing works great now.