Search code examples
c#wpfwpfdatagrid

WPF DataGrid (MultiSelector?) is raising SelectedItems CollectionChanged event multiple times


I'm not sure if this is an issue with the DataGrid control or with MultiSelectors in general, but when I select multiple rows within the grid, the CollectionChanged event is fired for every single row. This makes sense if I'm 'dragging' with my mouse, but it also occurs if I 'shift-click' to select multiple rows or simply click the top-left 'select-all-rows' button.

I have seen on the MultiSelector that there are Begin/EndUpdateSelectedItems methods as well as an IsUpdatingSelectedItems property. Unfortunately my consumer of this collection/event is unaware of its source.

Is there a way to make the DataGrid / SelectedItems collection only send the CollectionChanged notification when updating is finished?

thank you kindly.

Edit: I have found that for the DataGrid the IsUpdatingSelectedItems property is not being set even when changing a large selection.

Edit: I have found that the DataGrid SelectionChanged event is correctly fired only once after the full change. It's unfortunate since this ruins the possibility of simple data binding, but it is a potential workaround if you have control over the consumer of the SelectedItems collection.


Solution

  • For the sake of completeness, I'll 'answer' my own question. It turns out that the WPF controls in general can not handle anything but a single element change in their CollectionChanged event handlers - meaning that the 'call CollectionChanged for every item' workflow is the right way for the framework in its current form. However, personally I feel that this is a terrible performance issue.