Advertisement
gajda-ltd

RelayCommand

Feb 2nd, 2016
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.41 KB | None | 0 0
  1. namespace Core
  2. {
  3.     using System;
  4.     using System.Windows.Input;
  5.     /// <summary>
  6.     /// TODO: Update summary.
  7.     /// </summary>
  8.     public sealed class RelayCommand : ICommand
  9.     {
  10.         #region Fields
  11.         private readonly Action<object> execute;
  12.         private readonly Predicate<object> canExecute;
  13.         #endregion
  14.         #region Constructors
  15.         public RelayCommand(Action<object> execute)
  16.             : this(execute, null)
  17.         {
  18.         }
  19.         public RelayCommand(Action<object> execute, Predicate<object> canExecute)
  20.         {
  21.             if (execute == null)
  22.             {
  23.                 throw new ArgumentNullException("execute");
  24.             }
  25.             this.execute = execute;
  26.             this.canExecute = canExecute;
  27.         }
  28.         #endregion
  29.         #region ICommand Members [DebuggerStepThrough]
  30.         public bool CanExecute(object parameter)
  31.         {
  32.             return this.canExecute == null || this.canExecute(parameter);
  33.         }
  34.         public event EventHandler CanExecuteChanged
  35.         {
  36.             add
  37.             {
  38.                 CommandManager.RequerySuggested += value;
  39.             }
  40.             remove
  41.             {
  42.                 CommandManager.RequerySuggested -= value;
  43.             }
  44.         }
  45.         public void Execute(object parameter)
  46.         {
  47.             this.execute(parameter);
  48.         }
  49.         #endregion
  50.     }
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement