Skip to content

Fix CommandBar secondary visibility during overflow open - #21742

Merged
MrJul merged 1 commit into
AvaloniaUI:mainfrom
NathanDrake2406:nathan/fix-commandbar-secondary-visibility
Jul 10, 2026
Merged

Fix CommandBar secondary visibility during overflow open#21742
MrJul merged 1 commit into
AvaloniaUI:mainfrom
NathanDrake2406:nathan/fix-commandbar-secondary-visibility

Conversation

@NathanDrake2406

Copy link
Copy Markdown
Contributor

What does the pull request do?

Fixes a CommandBar overflow crash when a secondary command changes IsVisible while the overflow popup is realizing its items.

The scenario is reported in #21694: a CommandBarButton in CommandBar.SecondaryCommands can update IsVisible from a binding as it is added to the popup's ItemsControl, which re-enters the command bar overflow rebuild.

What is the current behavior?

Opening the overflow popup can throw:

InvalidOperationException: The control CommandBarButton already has a visual parent StackPanel while trying to add it as a child of StackPanel.

The re-entrant visibility update clears and rebuilds OverflowItems while PanelContainerGenerator.InsertContainer is still inserting the same control into the popup panel.

What is the updated/expected behavior with this PR?

Opening the overflow popup no longer mutates OverflowItems during the unsafe synchronous item-realization window. A visibility-triggered overflow rebuild is deferred until after Popup.IsOpen = true returns, then applied immediately so existing synchronous overflow state behavior is preserved outside that opening path.

How was the solution implemented (if it's not obvious)?

CommandBar now routes dynamic overflow rebuild requests through a small guard. While the overflow popup is synchronously opening, rebuild requests are marked as deferred. After the popup finishes opening, a single deferred rebuild is applied.

A regression test covers a secondary command that changes IsVisible as it becomes parented by the overflow presenter, reproducing the same visual-parent collision path without depending on a particular binding engine timing detail.

Checklist

Breaking changes

None.

Obsoletions / Deprecations

None.

Fixed issues

Fixes #21694

Verification

  • dotnet run --project tests/Avalonia.Controls.UnitTests/Avalonia.Controls.UnitTests.csproj -- --filter-method "*Open_DoesNotThrow_WhenSecondaryCommandVisibilityChangesDuringOverflowRealization"
  • dotnet run --project tests/Avalonia.Controls.UnitTests/Avalonia.Controls.UnitTests.csproj -- --filter-class "Avalonia.Controls.UnitTests.CommandBar*"
  • dotnet build src/Avalonia.Controls/Avalonia.Controls.csproj -p:AvsSkipBuildingLegacyTargetFrameworks=True
  • dotnet run --project tests/Avalonia.Controls.UnitTests/Avalonia.Controls.UnitTests.csproj

@NathanDrake2406
NathanDrake2406 marked this pull request as ready for review July 9, 2026 15:43
@MrJul MrJul added bug backport-candidate-12.1.x Consider this PR for backporting to 12.1 branch labels Jul 9, 2026
@avaloniaui-bot

Copy link
Copy Markdown

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

@MrJul MrJul left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM!

@MrJul
MrJul added this pull request to the merge queue Jul 10, 2026
Merged via the queue into AvaloniaUI:main with commit c00a2f2 Jul 10, 2026
12 checks passed
@MrJul MrJul added backported-12.1.x and removed backport-candidate-12.1.x Consider this PR for backporting to 12.1 branch labels Jul 29, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Having IsVisible bound on a CommandBarButton within CommandBar.SecondaryCommands throws InvalidOperationException when opening the overflow menu.

3 participants