dotnet / dotnet/wpf

ListCollectionView generating bad NewStartingIndex on LiveSorting

Open
#11,257 0 comments 2 reactions 0 assignees View on GitHub
Investigate
Dominant language
C#
Stars
7.7k
Forks
1.3k
Avg merge
1d 11h
Merged PRs (30d)
61

Description

When using a `ListCollectionView` with live sorting enabled, occasionally the `CollectionChanged` event will emit a `NotifyCollectionChangedAction.Move` notification where the `NotifyCollectionChangedEventArgs.NewStartingIndex` property doesn't match the actual index of the item in the collection found with `ListCollectionView.IndexOf(object item)`.

If the collection view is used as an items source for a WPF component such as `ListBox`, this can result in incorrect sorting behavior or duplicate items appearing.

This seems to only occur for large collections (hundreds of items) where most of them have been marked for re-sort, presumably enough to meet `ListCollectionView.LiveSortingDensityThreshold` and trigger the `LiveShapingList.RestoreLiveSortingByInsertionSort(...)` code path.

I've attached a failing unit test demonstrating the problem. The expected behavior is that observing `CollectionChanged` events would be enough to keep an external collection in sync with the `ListCollectionView` items.

[ListCollectionViewTests.cs.txt](https://github.com/user-attachments/files/23568895/ListCollectionViewTests.cs.txt)

Contributor guide

Open the contributing guide

Research direction

Start with the attached ListCollectionViewTests.cs.txt failing test and trace the LiveSorting path through LiveShapingList.RestoreLiveSortingByInsertionSort(...). Run the relevant WPF unit tests and compare CollectionChanged Move notifications with ListCollectionView.IndexOf(item); done means the reported NewStartingIndex matches the item's actual index for the large-collection scenario.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp
Domain
desktop
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.