WPF DataGrid (MultiSelector?) Raises the SelectedItems CollectionChanged event several times

I'm not sure if this is a problem with the DataGrid control or with MultiSelectors in general, but when I select multiple rows in the grid, the CollectionChanged event is fired for each individual row. It makes sense if I “drag and drop” with my mouse, but it also happens if I “change” to select several rows or just click the “Select all all rows” button in the upper left.

I saw on MultiSelector that there are Begin / EndUpdateSelectedItems methods, as well as the IsUpdatingSelectedItems property. Unfortunately, my consumer of this collection / event does not know its source.

Is there a way to get the DataGrid / SelectedItems collection to send a CollectionChanged notification only after the update is complete?

Thank you.

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

Edit: I found that the DataGrid SelectionChanged event correctly fires only once after a complete change. This is sad since it destroys the ability to simply bind data, but it is a potential workaround if you have consumer control over the SelectedItems collection.

+4
source share
2 answers

For completeness, I will answer my question. It turns out that WPF controls cannot handle anything at all except changing one element in CollectionChanged event handlers, which means that the callChanged workflow for each element is the right way for the framework in its current form. However, I personally feel this is a terrible performance issue.

+1
source

ViewModel:

private MultiSelector _selectedItems; Public MultiSelector SelectedItems { get {return _selectedItems; set { _selectedItems=value;} } 

Bind the SelectedItems property to the selected DataGrid and add System.Windows.Controls.Primitives.MultiSelector

0
source

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


All Articles