Fix for ListBox ghost items - #22161
Conversation
There was a problem hiding this comment.
🟡 Changes recommended
ScrollIntoView can now return a container instance that it has just recycled/removed, which can hand callers a control that is no longer in the panel’s visual tree.
Once you've addressed the issues Copilot identified, you can request another Copilot review.
Pull request overview
This PR addresses a virtualization edge case where ListBox/VirtualizingStackPanel can leave behind visible “ghost” item containers when scrolling while the control effectively has no usable width (e.g., hidden pane scenarios), and adds a regression test covering the reported repro from #15194.
Changes:
- Recycles the temporary
_scrollToElementwhen it fails to be adopted into the realized range, preventing it from remaining as a visible, unindexed internal child. - Adds a new unit test that simulates scrolling selection changes while the
ListBoxhasWidth = 0, then expanding it, asserting no visible unrealized containers appear.
File summaries
| File | Description |
|---|---|
src/Avalonia.Controls/VirtualizingStackPanel.cs |
Recycles an unadopted scroll-to container to prevent ghost elements after pane expansion. |
tests/Avalonia.Controls.UnitTests/ListBoxVirtualizationIssueTests.cs |
Adds regression coverage for zero-width scrolling + expand scenario to ensure no ghost containers remain visible. |
Review details
- Files reviewed: 2/2 changed files
- Comments generated: 1
- Review effort level: Lite
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
|
You can test this PR using the following package version. |
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
|
You can test this PR using the following package version. |
What does the pull request do?
Fixes an issue where a ListBox with 0 width can produce ghost items. See #15194 for a repro.
What is the current behavior?
A ListBox of size 0 can produce ghost items.
What is the updated/expected behavior with this PR?
No more ghost items.
How was the solution implemented (if it's not obvious)?
Ensure that those items are recycled.
Checklist
Breaking changes
Obsoletions / Deprecations
Fixed issues
Fixes #15194