public class RelayCommand<T> : ICommand
{
#region Private members
/// <summary>
/// Creates a new command that can always execute.
/// </summary>
private readonly Action<T> _execute;
/// <summary>
/// True if command is executing, false otherwise
/// </summary>
readonly Predicate<T> _canExecute = null;
#endregion
/// <summary>
/// Initializes a new instance of <see cref="RelayCommand"/>
/// </summary>
/// <param name="execute">The execution logic.</param>
/// <param name="canExecute">The execution status logic.</param>
public RelayCommand(Action<T> execute, Predicate<T> canExecute = null)
{
if (execute == null)
{
throw new ArgumentNullException("execute");
}
_execute = execute;
_canExecute = canExecute;
}
/// <summary>
/// Raised when RaiseCanExecuteChanged is called.
/// </summary>
public event EventHandler CanExecuteChanged
{
add { CommandManager.RequerySuggested += value; }
remove { CommandManager.RequerySuggested -= value; }
}
#region ICommand Members
/// <summary>
/// Determines whether this <see cref="RelayCommand"/> can execute in its current state.
/// </summary>
/// <param name="parameter">
/// Data used by the command. If the command does not require data to be passed, this object can be set to null.
/// </param>
/// <returns>True if this command can be executed; otherwise, false.
public bool CanExecute(object parameter)
{
return (_canExecute == null || _canExecute((T)parameter));
}
/// <summary>
/// Executes the <see cref="RelayCommand"/> on the current command target.
/// </summary>
/// <param name="parameter">
/// Data used by the command. If the command does not require data to be passed, this object can be set to null.
/// </param>
public void Execute(object parameter)
{
_execute((T)parameter);
}
#endregion ICommand Members
}
public class RelayCommand : RelayCommand
private ICommand _playMatch;
public ICommand PlayMatch
{
get
{
if (_playMatch == null)
_playMatch = new RelayCommand((p) => PlayMatch_Action());