Tosker

WPF Commands Part 2 - RelayCommand

Dec 13th, 2016
1,606
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.98 KB | None | 0 0
  1.     //www.toskerscorner.com
  2. public class RelayCommand : ICommand
  3.     {
  4.         readonly Action<object> _execute;
  5.         readonly Predicate<object> _canExecute;
  6.  
  7.         public RelayCommand(Action<object> execute, Predicate<object> canExecute)
  8.         {
  9.             if (execute == null)
  10.                 throw new ArgumentNullException("execute");
  11.  
  12.             _execute = execute;
  13.             _canExecute = canExecute;
  14.         }
  15.  
  16.         public RelayCommand(Action<object> execute) : this(execute, null)
  17.         {
  18.  
  19.         }
  20.  
  21.         public event EventHandler CanExecuteChanged
  22.         {
  23.             add { CommandManager.RequerySuggested += value; }
  24.             remove { CommandManager.RequerySuggested -= value; }
  25.         }
  26.  
  27.         public bool CanExecute(object parameter)
  28.         {
  29.             return _canExecute == null ? true : _canExecute(parameter);
  30.         }
  31.  
  32.         public void Execute(object parameter)
  33.         {
  34.             _execute(parameter);
  35.         }
  36.     }
Add Comment
Please, Sign In to add comment