Skip to content

Instantly share code, notes, and snippets.

@jfromaniello
Created November 18, 2009 10:29
Show Gist options
  • Save jfromaniello/237729 to your computer and use it in GitHub Desktop.
Save jfromaniello/237729 to your computer and use it in GitHub Desktop.
public class RelayCommand : RelayCommand<object>{
public RelayCommand(Action<object> execute) : base(execute)
{}
public RelayCommand(Action execute) : base(execute)
{}
public RelayCommand(Action execute, Func<bool> canExecute) : base(execute, canExecute)
{}
public RelayCommand(Action<object> execute, Predicate<object> canExecute) : base(execute, canExecute)
{}
}
public class RelayCommand<T> : ICommand
{
#region Fields
private readonly Predicate<T> _canExecute;
private readonly Action<T> _execute;
#endregion // Fields
#region Constructors
public RelayCommand(Action<T> execute)
: this(execute, null)
{ }
public RelayCommand(Action execute)
: this(o => execute())
{ }
public RelayCommand(Action execute, Func<bool> canExecute)
: this(o => execute(), o => canExecute())
{ }
public RelayCommand(Action<T> execute, Predicate<T> canExecute)
{
if (execute == null)
throw new ArgumentNullException("execute");
_execute = execute;
_canExecute = canExecute;
}
#endregion // Constructors
#region ICommand Members
[DebuggerStepThrough]
public bool CanExecute(object parameter)
{
return _canExecute == null ? true : _canExecute((T)parameter);
}
public event EventHandler CanExecuteChanged
{
add { CommandManager.RequerySuggested += value; }
remove { CommandManager.RequerySuggested -= value; }
}
public void Execute(object parameter)
{
_execute((T)parameter);
}
#endregion
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment