using System.Windows.Input; namespace JdeScoping.SecureStoreManager.ViewModels; /// /// A command implementation that delegates to action methods. /// public class RelayCommand : ICommand { private readonly Action _execute; private readonly Predicate? _canExecute; private EventHandler? _canExecuteChanged; /// /// Raised when the command's ability to execute may have changed. /// public event EventHandler? CanExecuteChanged { add => _canExecuteChanged += value; remove => _canExecuteChanged -= value; } /// /// Creates a new RelayCommand that can always execute. /// /// The action to execute. public RelayCommand(Action execute) : this(execute, null) { } /// /// Creates a new RelayCommand with a CanExecute predicate. /// /// The action to execute. /// The predicate to determine if the command can execute. public RelayCommand(Action execute, Predicate? canExecute) { _execute = execute ?? throw new ArgumentNullException(nameof(execute)); _canExecute = canExecute; } /// /// Creates a new RelayCommand from a parameterless action. /// /// The action to execute. public RelayCommand(Action execute) : this(_ => execute(), null) { } /// /// Creates a new RelayCommand from a parameterless action with a CanExecute predicate. /// /// The action to execute. /// The predicate to determine if the command can execute. public RelayCommand(Action execute, Func? canExecute) : this(_ => execute(), canExecute != null ? _ => canExecute() : null) { } /// /// Determines whether the command can execute. /// /// An optional command parameter. /// True if the command can execute, false otherwise. public bool CanExecute(object? parameter) { return _canExecute == null || _canExecute(parameter); } /// /// Executes the command with the specified parameter. /// /// An optional command parameter. public void Execute(object? parameter) { _execute(parameter); } /// /// Raises the CanExecuteChanged event. /// public void RaiseCanExecuteChanged() { _canExecuteChanged?.Invoke(this, EventArgs.Empty); } }