Skip to content

Commit f4addd5

Browse files
committed
fix ghost items
1 parent a853fd6 commit f4addd5

1 file changed

Lines changed: 15 additions & 2 deletions

File tree

src/Avalonia.Controls/VirtualizingStackPanel.cs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -700,8 +700,21 @@ protected internal override int IndexFromContainer(Control container)
700700
// hence the width extent should be correct now, and we can try to scroll again.
701701
scrollToElement.BringIntoView();
702702

703-
_scrollToElement = null;
704-
_scrollToIndex = -1;
703+
// The layout pass normally adopts the temporary element into the realized range.
704+
// When it doesn't (for example, while its containing pane has no usable width),
705+
// it is still an internal child. Recycle it before dropping the reference so it
706+
// cannot remain as a visible, unindexed "ghost" element.
707+
if (_scrollToElement is { } unadoptedScrollToElement)
708+
{
709+
var unadoptedScrollToIndex = _scrollToIndex;
710+
_scrollToElement = null;
711+
_scrollToIndex = -1;
712+
RecycleElement(unadoptedScrollToElement, unadoptedScrollToIndex);
713+
}
714+
else
715+
{
716+
_scrollToIndex = -1;
717+
}
705718
return scrollToElement;
706719
}
707720

0 commit comments

Comments
 (0)