You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
ListView does not properly update container indices when items are inserted/removed #4165
When items are inserted/removed in an inner position of a ObservableCollection bound to ListView.ItemsSource, the existing items that "move" because of the change don't have their container indices updated properly.
Consider the following flow:
Create source collection with one item "2"
Bind to ItemsSource of ListView
Insert item "1" as the first item of the collection (Insert(0, "1"))
Now observe the MaterializedContainers returns 0 as the IndexForItemContainerProperty value for both containers.
This behavior breaks various code paths in ListView, where the property is used (IndexForContainer method, ItemForContainer method, etc.).
Expected behavior
The IndexForItemContainerProperty should be properly updated when source collection changes.
How to reproduce it (as minimally and precisely as possible)
var source = new ObservableCollection<string>();
var SUT = new ListView { ItemsSource = source };
WindowHelper.WindowContent = SUT;
source.Add("test");
await WindowHelper.WaitForIdle();
source.Insert(0, "different");
await WindowHelper.WaitForIdle();
var containerIndices = SUT.MaterializedContainers
.Select(container => container.GetValue(ItemsControl.IndexForItemContainerProperty))
.OfType<int>()
.OrderBy(index => index)
.ToArray();
CollectionAssert.AreEqual(new int[] { 0, 1 }, containerIndices);
Activity