Browse Source

Fix overlapping items in `VirtualizingStackPanel` (#13765)

* Added failing test for #12744.

* Inserting items makes the StartU unstable.

This ports the fix in https://github.com/AvaloniaUI/Avalonia.Controls.TreeDataGrid/pull/229 to Avalonia.

Fixes #12744
pull/13773/head
Steven Kirk 3 years ago
committed by GitHub
parent
commit
b630f21b7e
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 1
      src/Avalonia.Controls/Utils/RealizedStackElements.cs
  2. 22
      tests/Avalonia.Controls.UnitTests/VirtualizingStackPanelTests.cs

1
src/Avalonia.Controls/Utils/RealizedStackElements.cs

@ -294,6 +294,7 @@ namespace Avalonia.Controls.Utils
{
// The insertion point was before the first element, update the first index.
_firstIndex += count;
_startUUnstable = true;
}
else
{

22
tests/Avalonia.Controls.UnitTests/VirtualizingStackPanelTests.cs

@ -843,6 +843,28 @@ namespace Avalonia.Controls.UnitTests
Assert.Equal(1, panel.VisualChildren.Count);
}
[Fact]
public void Inserting_Item_Before_Viewport_Preserves_FirstRealizedIndex()
{
// Issue #12744
using var app = App();
var (target, scroll, itemsControl) = CreateTarget();
var items = (IList)itemsControl.ItemsSource!;
// Scroll down 20 items.
scroll.Offset = new Vector(0, 200);
target.UpdateLayout();
Assert.Equal(20, target.FirstRealizedIndex);
// Insert an item at the beginning.
items.Insert(0, "New Item");
target.UpdateLayout();
// The first realized index should still be 20 as the scroll should be unchanged.
Assert.Equal(20, target.FirstRealizedIndex);
Assert.Equal(new(0, 200), scroll.Offset);
}
private static IReadOnlyList<int> GetRealizedIndexes(VirtualizingStackPanel target, ItemsControl itemsControl)
{
return target.GetRealizedElements()

Loading…
Cancel
Save