qamp/Qrakhen.Qamp.Editor/Commands/RelayCommand.cs

30 lines
1009 B
C#

using System.Windows.Input;
namespace Qrakhen.Qamp.Editor.Commands;
public class RelayCommand : ICommand
{
private readonly Action<object?> _execute;
private readonly Func<object?, bool> _canExecute;
public RelayCommand(Action<object?> execute) : this(execute, null) { }
public RelayCommand(Action<object?> execute, Func<object?, bool>? canExecute)
{
_execute = execute ?? throw new ArgumentNullException(nameof(execute));
_canExecute = canExecute ?? (p => true);
}
public RelayCommand(Action execute) : this(p => execute()) { }
public RelayCommand(Action execute, Func<bool> canExecute) : this(p => execute(), p => canExecute()) { }
public event EventHandler? CanExecuteChanged {
add => CommandManager.RequerySuggested += value;
remove => CommandManager.RequerySuggested -= value;
}
public bool CanExecute(object? parameter) => _canExecute(parameter);
public void Execute(object? parameter) => _execute(parameter);
}