public class PrintCommand:ICommand
{
private readonly Action<object> _execute;
private readonly Predicate<object> _canExecute;
/// <summary>
/// Default constructor to execute an action.
/// </summary>
/// <param name="execute">action to execute.</param>
public PrintCommand(Action<object> execute)
: this(execute, null)
{
}
/// <summary>
/// Constructor to execute an action only on approval.
/// </summary>
/// <param name="execute">action to execute.</param>
/// <param name="canExecute">whether to execute or not</param>
public PrintCommand(Action<object> execute, Predicate<object> canExecute)
{
if (execute == null)
throw new ArgumentNullException("execute");
_execute = execute;
_canExecute = canExecute;
}
/// <summary>
/// Gets whether to execute the action or not.
/// </summary>
/// <param name="parameter">element that undergoes action.</param>
/// <returns>a boolean value</returns>
public bool CanExecute(object parameter)
{
return _canExecute == null ? true : _canExecute(parameter);
}
/// <summary>
/// An event that determines whether the proposed execution can be changed.
/// </summary>
public event EventHandler CanExecuteChanged
{
add { CommandManager.RequerySuggested += value; }
remove { CommandManager.RequerySuggested -= value; }
}
/// <summary>
/// A method to execute action.
/// </summary>
/// <param name="parameter">object to incur action.</param>
public void Execute(object parameter)
{
_execute(parameter);
}
}
}
|