|
|
@@ -93,6 +93,7 @@ namespace Avalonia.Controls
|
|
|
static MenuItem()
|
|
|
{
|
|
|
SelectableMixin.Attach<MenuItem>(IsSelectedProperty);
|
|
|
+ CommandProperty.Changed.Subscribe(CommandChanged);
|
|
|
FocusableProperty.OverrideDefaultValue<MenuItem>(true);
|
|
|
IconProperty.Changed.AddClassHandler<MenuItem>(x => x.IconChanged);
|
|
|
ItemsPanelProperty.OverrideDefaultValue<MenuItem>(DefaultPanel);
|
|
|
@@ -424,6 +425,40 @@ namespace Avalonia.Controls
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ /// <summary>
|
|
|
+ /// Called when the <see cref="Command"/> property changes.
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="e">The event args.</param>
|
|
|
+ private static void CommandChanged(AvaloniaPropertyChangedEventArgs e)
|
|
|
+ {
|
|
|
+ if (e.Sender is MenuItem menuItem)
|
|
|
+ {
|
|
|
+ if (e.OldValue is ICommand oldCommand)
|
|
|
+ {
|
|
|
+ oldCommand.CanExecuteChanged -= menuItem.CanExecuteChanged;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (e.NewValue is ICommand newCommand)
|
|
|
+ {
|
|
|
+ newCommand.CanExecuteChanged += menuItem.CanExecuteChanged;
|
|
|
+ }
|
|
|
+
|
|
|
+ menuItem.CanExecuteChanged(menuItem, EventArgs.Empty);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// Called when the <see cref="ICommand.CanExecuteChanged"/> event fires.
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="sender">The event sender.</param>
|
|
|
+ /// <param name="e">The event args.</param>
|
|
|
+ private void CanExecuteChanged(object sender, EventArgs e)
|
|
|
+ {
|
|
|
+ // HACK: Just set the IsEnabled property for the moment. This needs to be changed to
|
|
|
+ // use IsEnabledCore etc. but it will do for now.
|
|
|
+ IsEnabled = Command == null || Command.CanExecute(CommandParameter);
|
|
|
+ }
|
|
|
+
|
|
|
/// <summary>
|
|
|
/// Called when the <see cref="Icon"/> property changes.
|
|
|
/// </summary>
|