Advertisement
MrModest

RelayCommand

Mar 8th, 2017
198
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.28 KB | None | 0 0
  1.     public class RelayCommand : ICommand
  2.     {
  3.         private readonly Action _execute;
  4.         private readonly Predicate<bool> _canExecute; //изменил Predicate на Predicate<bool>
  5.  
  6.         public event EventHandler CanExecuteChanged;
  7.  
  8.         public RelayCommand(Action execute) : this(execute, null) { }
  9.  
  10.         public RelayCommand(Action execute, Predicate<bool> canExecute) //изменил Predicate на Predicate<bool>
  11.         {
  12.             if (execute == null)
  13.             {
  14.                 throw new ArgumentNullException("предикат не должен быть равным нулю");
  15.             }
  16.             _execute = execute;
  17.             _canExecute = canExecute;
  18.         }
  19.  
  20.         public bool CanExecute(object parameter)
  21.         {
  22.             return _canExecute == null ? true : _canExecute((bool)parameter); //добавил параметр в аргументы с явным преобразованием
  23.         }
  24.  
  25.         public void Execute(object parameter)
  26.         {
  27.             _execute();
  28.         }
  29.  
  30.         public void RaiseCanExecuteChanged()
  31.         {
  32.             var handler = CanExecuteChanged;
  33.             if (handler != null)
  34.             {
  35.                 handler(this, EventArgs.Empty);
  36.             }
  37.         }
  38.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement