using System; using System.Windows.Input; namespace ModernKeePass.Common { /// /// A command whose sole purpose is to relay its functionality /// to other objects by invoking delegates. /// The default return value for the CanExecute method is 'true'. /// needs to be called whenever /// is expected to return a different value. /// public class RelayCommand : ICommand { private readonly Action _execute; private readonly Func _canExecute; /// /// Raised when RaiseCanExecuteChanged is called. /// public event EventHandler CanExecuteChanged; /// /// Creates a new command that can always execute. /// /// The execution logic. public RelayCommand(Action execute) : this(execute, null) { } /// /// Creates a new command. /// /// The execution logic. /// The execution status logic. public RelayCommand(Action execute, Func canExecute) { if (execute == null) throw new ArgumentNullException(nameof(execute)); _execute = execute; _canExecute = canExecute; } /// /// Determines whether this can execute in its current state. /// /// /// Data used by the command. If the command does not require data to be passed, this object can be set to null. /// /// true if this command can be executed; otherwise, false. public bool CanExecute(object parameter) { return _canExecute?.Invoke() ?? true; } /// /// Executes the on the current command target. /// /// /// Data used by the command. If the command does not require data to be passed, this object can be set to null. /// public void Execute(object parameter) { _execute(); } /// /// Method used to raise the event /// to indicate that the return value of the /// method has changed. /// public void RaiseCanExecuteChanged() { CanExecuteChanged?.Invoke(this, EventArgs.Empty); } } }