Advertisement
Guest User

Untitled

a guest
Jul 5th, 2022
31
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.03 KB | None | 0 0
  1. namespace RelayCommandSample
  2. {
  3.     using System;
  4.     using System.Windows.Input;
  5.  
  6.     public class RelayCommand : ICommand
  7.     {
  8.         private readonly Action<object> execute;
  9.         private readonly Predicate<object> canExecute;
  10.  
  11.         public RelayCommand(Action<object> execute)
  12.             : this(execute, null)
  13.         {
  14.         }
  15.  
  16.         public RelayCommand(Action<object> execute, Predicate<object> canExecute)
  17.         {
  18.             this.execute = execute ?? throw new ArgumentNullException(nameof(execute));
  19.             this.canExecute = canExecute ?? (o => true);
  20.         }
  21.  
  22.         public event EventHandler CanExecuteChanged
  23.         {
  24.             add => CommandManager.RequerySuggested += value;
  25.             remove => CommandManager.RequerySuggested -= value;
  26.         }
  27.  
  28.         public bool CanExecute(object parameter)
  29.         {
  30.             return this.canExecute(parameter);
  31.         }
  32.  
  33.         public void Execute(object parameter)
  34.         {
  35.             this.execute(parameter);
  36.         }
  37.     }
  38. }
  39.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement