Skip to content

Fix for ListBox ghost items - #22161

Open
timunie wants to merge 3 commits into
AvaloniaUI:mainfrom
timunie:fix/listbox-ghost-items
Open

Fix for ListBox ghost items#22161
timunie wants to merge 3 commits into
AvaloniaUI:mainfrom
timunie:fix/listbox-ghost-items

Conversation

@timunie

@timunie timunie commented Sep 7, 2026

Copy link
Copy Markdown
Collaborator

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

@MrJul MrJul added bug backport-candidate-12.1.x Consider this PR for backporting to 12.1 branch labels Sep 7, 2026
@timunie
timunie requested a lite review from Copilot September 7, 2026 08:43

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 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 _scrollToElement when 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 ListBox has Width = 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.

Comment thread src/Avalonia.Controls/VirtualizingStackPanel.cs
@avaloniaui-bot

Copy link
Copy Markdown

You can test this PR using the following package version. 12.2.999-cibuild0069503-alpha. (feed url: https://nuget-feed-all.avaloniaui.net/v3/index.json) [PRBUILDID]

Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
@avaloniaui-bot

Copy link
Copy Markdown

You can test this PR using the following package version. 12.2.999-cibuild0069522-alpha. (feed url: https://nuget-feed-all.avaloniaui.net/v3/index.json) [PRBUILDID]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

backport-candidate-12.1.x Consider this PR for backporting to 12.1 branch bug

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Issue with when scrolling a ListBox/VirtualizingStackPanel when hidden

4 participants