Advertisement
Guest User

RelayCommand.cs

a guest
Sep 28th, 2020
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.03 KB | None | 0 0
  1. public class RelayCommand<T> : ICommand
  2.     {
  3.         private readonly Action<T> _execute = null;
  4.         private readonly Func<T, bool> _canExecute = null;
  5.  
  6.         public RelayCommand(Action<T> execute, Func<T, bool> canExecute = null)
  7.         {
  8.             _execute = execute ?? throw new ArgumentNullException(nameof(execute));
  9.             _canExecute = canExecute ?? (_ => true);
  10.         }
  11.  
  12.         public event EventHandler CanExecuteChanged
  13.         {
  14.             add => CommandManager.RequerySuggested += value;
  15.             remove => CommandManager.RequerySuggested -= value;
  16.         }
  17.  
  18.         public bool CanExecute(object parameter) => _canExecute((T)parameter);
  19.  
  20.         public void Execute(object parameter) => _execute((T)parameter);
  21.     }
  22.  
  23.     public class RelayCommand : RelayCommand<object>
  24.     {
  25.         public RelayCommand(Action execute)
  26.             : base(_ => execute()) { }
  27.  
  28.         public RelayCommand(Action execute, Func<bool> canExecute)
  29.             : base(_ => execute(), _ => canExecute()) { }
  30.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement