Skip to content
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

Button state update when CommandParameter changed #5772

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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 13 additions & 0 deletions src/Avalonia.Controls/Button.cs
Expand Up @@ -96,6 +96,7 @@ static Button()
{
FocusableProperty.OverrideDefaultValue(typeof(Button), true);
CommandProperty.Changed.Subscribe(CommandChanged);
CommandParameterProperty.Changed.Subscribe(CommandParameterChanged);
IsDefaultProperty.Changed.Subscribe(IsDefaultChanged);
IsCancelProperty.Changed.Subscribe(IsCancelChanged);
}
Expand Down Expand Up @@ -418,6 +419,18 @@ private static void CommandChanged(AvaloniaPropertyChangedEventArgs e)
}
}

/// <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 Button button)
{
button.CanExecuteChanged(button, EventArgs.Empty);
}
}

/// <summary>
/// Called when the <see cref="IsDefault"/> property changes.
/// </summary>
Expand Down
33 changes: 27 additions & 6 deletions tests/Avalonia.Controls.UnitTests/ButtonTests.cs
Expand Up @@ -269,6 +269,19 @@ public void Button_Unsubscribes_From_Command_CanExecuteChanged_When_Removed_From
Assert.Equal(0, command.SubscriptionCount);
}

[Fact]
public void Button_Invokes_CanExecute_When_CommandParameter_Changed()
{
var command = new TestCommand(p => p is bool value && value);
var target = new Button { Command = command };

target.CommandParameter = true;
Assert.True(target.IsEffectivelyEnabled);

target.CommandParameter = false;
Assert.False(target.IsEffectivelyEnabled);
}

private class TestButton : Button, IRenderRoot
{
public TestButton()
Expand Down Expand Up @@ -324,12 +337,22 @@ private void RaisePointerMove(Button button, Point pos)

private class TestCommand : ICommand
{
private readonly Func<object, bool> _canExecute;
private readonly Action<object> _execute;
private EventHandler _canExecuteChanged;
private bool _enabled;
private bool _enabled = true;

public TestCommand(bool enabled)
public TestCommand(bool enabled = true)
{
_enabled = enabled;
_canExecute = _ => _enabled;
_execute = _ => { };
}

public TestCommand(Func<object, bool> canExecute, Action<object> execute = null)
{
_canExecute = canExecute;
_execute = execute ?? (_ => { });
}

public bool IsEnabled
Expand All @@ -353,11 +376,9 @@ public bool IsEnabled
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);
}
}
}