Browse Source

Merge pull request #5378 from YohDeadfall/reevaluate-can-execute-on-param-change

CanExecute reevaluation on parameter change
pull/5451/head
Steven Kirk 5 years ago
committed by GitHub
parent
commit
610244d742
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 13
      src/Avalonia.Controls/MenuItem.cs
  2. 30
      tests/Avalonia.Controls.UnitTests/MenuItemTests.cs

13
src/Avalonia.Controls/MenuItem.cs

@ -112,6 +112,7 @@ namespace Avalonia.Controls
SelectableMixin.Attach<MenuItem>(IsSelectedProperty); SelectableMixin.Attach<MenuItem>(IsSelectedProperty);
PressedMixin.Attach<MenuItem>(); PressedMixin.Attach<MenuItem>();
CommandProperty.Changed.Subscribe(CommandChanged); CommandProperty.Changed.Subscribe(CommandChanged);
CommandParameterProperty.Changed.Subscribe(CommandParameterChanged);
FocusableProperty.OverrideDefaultValue<MenuItem>(true); FocusableProperty.OverrideDefaultValue<MenuItem>(true);
HeaderProperty.Changed.AddClassHandler<MenuItem>((x, e) => x.HeaderChanged(e)); HeaderProperty.Changed.AddClassHandler<MenuItem>((x, e) => x.HeaderChanged(e));
IconProperty.Changed.AddClassHandler<MenuItem>((x, e) => x.IconChanged(e)); IconProperty.Changed.AddClassHandler<MenuItem>((x, e) => x.IconChanged(e));
@ -506,6 +507,18 @@ namespace Avalonia.Controls
} }
} }
/// <summary>
/// Called when the <see cref="CommandParameter"/> property changes.
/// </summary>
/// <param name="e">The event args.</param>
private static void CommandParameterChanged(AvaloniaPropertyChangedEventArgs e)
{
if (e.Sender is MenuItem menuItem)
{
menuItem.CanExecuteChanged(menuItem, EventArgs.Empty);
}
}
/// <summary> /// <summary>
/// Called when the <see cref="ICommand.CanExecuteChanged"/> event fires. /// Called when the <see cref="ICommand.CanExecuteChanged"/> event fires.
/// </summary> /// </summary>

30
tests/Avalonia.Controls.UnitTests/MenuItemTests.cs

@ -156,15 +156,35 @@ namespace Avalonia.Controls.UnitTests
root.Child = null; root.Child = null;
Assert.Equal(0, command.SubscriptionCount); Assert.Equal(0, command.SubscriptionCount);
} }
[Fact]
public void MenuItem_Invokes_CanExecute_When_CommandParameter_Changed()
{
var command = new TestCommand(p => p is bool value && value);
var target = new MenuItem { Command = command };
target.CommandParameter = true;
Assert.True(target.IsEffectivelyEnabled);
target.CommandParameter = false;
Assert.False(target.IsEffectivelyEnabled);
}
private class TestCommand : ICommand private class TestCommand : ICommand
{ {
private bool _enabled; private readonly Func<object, bool> _canExecute;
private readonly Action<object> _execute;
private EventHandler _canExecuteChanged; private EventHandler _canExecuteChanged;
public TestCommand(bool enabled = true) public TestCommand(bool enabled = true)
: this(_ => enabled, _ => { })
{
}
public TestCommand(Func<object, bool> canExecute, Action<object> execute = null)
{ {
_enabled = enabled; _canExecute = canExecute;
_execute = execute ?? (_ => { });
} }
public int SubscriptionCount { get; private set; } public int SubscriptionCount { get; private set; }
@ -175,11 +195,9 @@ namespace Avalonia.Controls.UnitTests
remove { _canExecuteChanged -= value; --SubscriptionCount; } remove { _canExecuteChanged -= value; --SubscriptionCount; }
} }
public bool CanExecute(object parameter) => _enabled; public bool CanExecute(object parameter) => _canExecute(parameter);
public void Execute(object parameter) public void Execute(object parameter) => _execute(parameter);
{
}
} }
} }
} }

Loading…
Cancel
Save