Skip to content

Instantly share code, notes, and snippets.

@ginomessmer
Created December 7, 2016 17:28
Show Gist options
  • Save ginomessmer/9763d32243f8d13963cfe328ae7966fd to your computer and use it in GitHub Desktop.
Save ginomessmer/9763d32243f8d13963cfe328ae7966fd to your computer and use it in GitHub Desktop.
DelegateCommand for MVVM
public class DelegateCommand<T> : ICommand where T : class
{
private readonly Predicate<T> _canExecute;
private readonly Action<T> _execute;
public DelegateCommand(Action<T> execute)
: this(execute, null)
{
}
public DelegateCommand(Action<T> execute, Predicate<T> canExecute)
{
_execute = execute;
_canExecute = canExecute;
}
public bool CanExecute(object parameter)
{
if (_canExecute == null)
return true;
return _canExecute((T)parameter);
}
public void Execute(object parameter)
{
_execute((T)parameter);
}
public event EventHandler CanExecuteChanged;
public void RaiseCanExecuteChanged()
{
CanExecuteChanged?.Invoke(this, EventArgs.Empty);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment