Skip to content

Instantly share code, notes, and snippets.

@Konctantin
Last active January 25, 2016 13:40
Show Gist options
  • Select an option

  • Save Konctantin/103200c1994d32402c05 to your computer and use it in GitHub Desktop.

Select an option

Save Konctantin/103200c1994d32402c05 to your computer and use it in GitHub Desktop.
public class RelayCommand<T> : ICommand
{
Action<T> exec;
Predicate<T> canexec;
public RelayCommand(Action<T> exec, Predicate<T> canexec = null)
{
Debug.Assert(exec != null);
this.exec = exec;
this.canexec = canexec;
}
public event EventHandler CanExecuteChanged
{
add { CommandManager.RequerySuggested += value; }
remove { CommandManager.RequerySuggested -= value; }
}
public bool CanExecute(object parameter) => canexec?.Invoke((T)parameter) != false;
public void Execute(object parameter) => exec((T)parameter);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment