using System; using System.Collections.Generic; using System.Text; using System.Windows.Input; namespace RGBController2.Commands { public class ActionCommand : ICommand { private readonly Action action; private readonly Predicate predicate; /// /// Initializes a new instance of the class. /// /// The action to invoke on command. public ActionCommand(Action action) : this(action, null) { } /// /// Initializes a new instance of the class. /// /// The action to invoke on command. /// The predicate that determines if the action can be invoked. public ActionCommand(Action action, Predicate predicate) { if (action == null) { throw new ArgumentNullException(nameof(action), @"You must specify an Action."); } this.action = action; this.predicate = predicate; } /// /// Occurs when the detects conditions that might change the ability of a command to execute. /// public event EventHandler CanExecuteChanged { add { CommandManager.RequerySuggested += value; } remove { CommandManager.RequerySuggested -= value; } } /// /// Determines whether the command can execute. /// /// A custom parameter object. /// /// Returns true if the command can execute, otherwise returns false. /// public bool CanExecute(object parameter) { if (this.predicate == null) { return true; } return this.predicate(parameter); } /// /// Executes the command. /// public void Execute() { Execute(null); } /// /// Executes the command. /// /// A custom parameter object. public void Execute(object parameter) { this.action(parameter); } } }