Skip to content

ListView does not properly update container indices when items are inserted/removed #4165

Description

@MartinZikmund

Current behavior

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:

  1. Create source collection with one item "2"
  2. Bind to ItemsSource of ListView
  3. Insert item "1" as the first item of the collection (Insert(0, "1"))
  4. 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);

Workaround

Environment

Nuget Package:

  • Uno.UI / Uno.UI.WebAssembly / Uno.UI.Skia
  • Uno.WinUI / Uno.WinUI.WebAssembly / Uno.WinUI.Skia
  • Uno.SourceGenerationTasks
  • Uno.UI.RemoteControl / Uno.WinUI.RemoteControl
  • Other:

Nuget Package Version(s):

Affected platform(s):

  • iOS
  • Android
  • WebAssembly
  • WebAssembly renderers for Xamarin.Forms
  • macOS
  • Skia
    • WPF
    • GTK (Linux)
    • Tizen
  • Windows
  • Build tasks
  • Solution Templates

IDE:

  • Visual Studio 2017 (version: )
  • Visual Studio 2019 (version: )
  • Visual Studio for Mac (version: )
  • Rider Windows (version: )
  • Rider macOS (version: )
  • Visual Studio Code (version: )

Relevant plugins:

  • Resharper (version: )

Anything else we need to know?

Activity

  1. added
    kind/bug
    triage/untriaged
    project/items 🧾
    on Sep 28, 2020
  2. added this to the 3.1 milestone on Sep 30, 2020
  3. removed
    triage/untriaged
    on Sep 30, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    kind/bugproject/items 🧾

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions