Browse Source

Merge pull request #11432 from AvaloniaUI/fixes/virtualizingstackpanel-scrollintoview-ghost-items

Fix ghost elements in VirtualizingStackPanel
pull/11443/head
Max Katz 3 years ago
committed by GitHub
parent
commit
d6e5c5100a
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 2
      src/Avalonia.Controls/VirtualizingStackPanel.cs
  2. 34
      tests/Avalonia.Controls.UnitTests/VirtualizingStackPanelTests.cs

2
src/Avalonia.Controls/VirtualizingStackPanel.cs

@ -343,7 +343,7 @@ namespace Avalonia.Controls
{
var items = Items;
if (_isInLayout || index < 0 || index >= items.Count || _realizedElements is null)
if (_isInLayout || index < 0 || index >= items.Count || _realizedElements is null || !IsEffectivelyVisible)
return null;
if (GetRealizedElement(index) is Control element)

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

@ -657,6 +657,40 @@ namespace Avalonia.Controls.UnitTests
root.LayoutManager.ExecuteLayoutPass();
}
[Fact]
public void ScrollIntoView_On_Effectively_Invisible_Panel_Does_Not_Create_Ghost_Elements()
{
var items = new[] { "foo", "bar", "baz" };
var (target, _, itemsControl) = CreateUnrootedTarget(items: items);
var container = new Decorator { Margin = new Thickness(100), Child = itemsControl };
var root = new TestRoot(true, container);
root.LayoutManager.ExecuteInitialLayoutPass();
// Clear the items and do a layout to recycle all elements.
itemsControl.ItemsSource = null;
root.LayoutManager.ExecuteLayoutPass();
// Should have no realized elements and 3 unrealized elements.
Assert.Equal(0, target.GetRealizedElements().Count);
Assert.Equal(3, target.Children.Count);
// Make the panel effectively invisible and set items.
container.IsVisible = false;
itemsControl.ItemsSource = items;
// Try to scroll into view while effectively invisible.
target.ScrollIntoView(0);
// Make the panel visible and layout.
container.IsVisible = true;
root.LayoutManager.ExecuteLayoutPass();
// Should have 3 realized elements and no unrealized elements.
Assert.Equal(3, target.GetRealizedElements().Count);
Assert.Equal(3, target.Children.Count);
}
private static IReadOnlyList<int> GetRealizedIndexes(VirtualizingStackPanel target, ItemsControl itemsControl)
{
return target.GetRealizedElements()

Loading…
Cancel
Save