Skip to content

Fix MenuItem enabled state in the presence of sub-items. #18679

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 19, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion src/Avalonia.Controls/MenuItem.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Windows.Input;
using Avalonia.Automation;
Expand Down Expand Up @@ -341,7 +342,7 @@ public string? GroupName
/// <inheritdoc/>
IMenuElement? IMenuItem.Parent => Parent as IMenuElement;

protected override bool IsEnabledCore => base.IsEnabledCore && _commandCanExecute;
protected override bool IsEnabledCore => base.IsEnabled && (HasSubMenu || _commandCanExecute);
Copy link
Member

Choose a reason for hiding this comment

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

Is the change from base.IsEnabledCore to base.IsEnabled wanted? The logic stays the same (currently base.IsEnabledCore returns IsEnabled), but it seems a little odd to not refer to the base property.


/// <inheritdoc/>
bool IMenuElement.MoveSelection(NavigationDirection direction, bool wrap) => MoveSelection(direction, wrap);
Expand Down Expand Up @@ -710,6 +711,15 @@ protected override void OnPropertyChanged(AvaloniaPropertyChangedEventArgs chang
{
GroupNameChanged(change);
}
else if (change.Property == ItemCountProperty)
{
// A menu item with no sub-menu is effectively disabled if its command binding
// failed: this means that the effectively enabled state depends on whether the
// number of items in the menu is 0 or not.
var (o, n) = change.GetOldAndNewValue<int>();
if (o == 0 || n == 0)
UpdateIsEffectivelyEnabled();
}
}
/// <summary>
/// Called when the <see cref="GroupName"/> property changes.
Expand Down
39 changes: 39 additions & 0 deletions tests/Avalonia.Controls.UnitTests/MenuItemTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,45 @@ public void MenuItem_Is_Disabled_When_Bound_Command_Doesnt_Exist()
Assert.False(target.IsEffectivelyEnabled);
}

[Fact]
public void MenuItem_With_Styled_Command_Binding_Should_Be_Enabled_With_Child_Missing_Command()
{
using var app = Application();

var viewModel = new MenuViewModel("Parent")
{
Children = [new MenuViewModel("Child")]
};

var contextMenu = new ContextMenu
{
ItemsSource = new[] { viewModel },
Styles =
{
new Style(x => x.OfType<MenuItem>())
{
Setters =
{
new Setter(MenuItem.HeaderProperty, new Binding("Header")),
new Setter(MenuItem.ItemsSourceProperty, new Binding("Children")),
new Setter(MenuItem.CommandProperty, new Binding("Command"))
}
}
}
};

var window = new Window { ContextMenu = contextMenu };
window.Show();
contextMenu.Open();

var parentMenuItem = Assert.IsType<MenuItem>(contextMenu.ContainerFromIndex(0));

Assert.Same(parentMenuItem.DataContext, viewModel);
Assert.Same(parentMenuItem.ItemsSource, viewModel.Children);
Assert.True(parentMenuItem.IsEnabled);
Assert.True(parentMenuItem.IsEffectivelyEnabled);
}

[Fact]
public void MenuItem_Is_Disabled_When_Bound_Command_Is_Removed()
{
Expand Down
Loading