using System; using System.Windows.Input; namespace MVVM { public class ActionCommand : ICommand { private Action action; private Predicate predicate; public ActionCommand(Action action) : this(action, null) { } public ActionCommand(Action action, Predicate predicate) { this.action = action ?? throw new ArgumentNullException(nameof(action)); this.predicate = predicate; } #region ICommand members: public event EventHandler CanExecuteChanged { add { CommandManager.RequerySuggested += value; } remove { CommandManager.RequerySuggested -= value; } } public bool CanExecute(object parameter) => predicate == null || predicate(parameter); public void Execute() => Execute(null); public void Execute(object parameter) => action(parameter); #endregion } }