diff --git a/src/Avalonia.Controls/MenuItem.cs b/src/Avalonia.Controls/MenuItem.cs
index 45fc2ed859..1670e496b4 100644
--- a/src/Avalonia.Controls/MenuItem.cs
+++ b/src/Avalonia.Controls/MenuItem.cs
@@ -310,12 +310,7 @@ namespace Avalonia.Controls
protected override bool IsEnabledCore => base.IsEnabledCore && _commandCanExecute;
///
- bool IMenuElement.MoveSelection(NavigationDirection direction, bool wrap)
- {
- if (Presenter?.Panel is null)
- (VisualRoot as ILayoutRoot)?.LayoutManager.ExecuteLayoutPass();
- return MoveSelection(direction, wrap);
- }
+ bool IMenuElement.MoveSelection(NavigationDirection direction, bool wrap) => MoveSelection(direction, wrap);
///
IMenuItem? IMenuElement.SelectedItem
@@ -686,6 +681,12 @@ namespace Avalonia.Controls
/// The event args.
private void PopupOpened(object? sender, EventArgs e)
{
+ // If we're using overlay popups, there's a chance we need to do a layout pass before
+ // the child items are added to the visual tree. If we don't do this here, then
+ // selection breaks.
+ if (Presenter?.IsAttachedToVisualTree == false)
+ UpdateLayout();
+
var selected = SelectedIndex;
if (selected != -1)
@@ -705,6 +706,11 @@ namespace Avalonia.Controls
SelectedItem = null;
}
+ private void UpdateLayout()
+ {
+ (VisualRoot as ILayoutRoot)?.LayoutManager.ExecuteLayoutPass();
+ }
+
void ICommandSource.CanExecuteChanged(object sender, EventArgs e) => this.CanExecuteChanged(sender, e);
void IClickableControl.RaiseClick()