gajda-ltd icon

RelayCommand

gajda-ltd | PRO | 02/02/16 10:51:22 AM UTC | 0 ⭐ | 243 👁️ | Never ⏰ | []
C# |

1.41 KB

|

None

|

0 👍

/

0 👎

namespace Core
{
    using System;
    using System.Windows.Input;
    /// <summary>
    /// TODO: Update summary.
    /// </summary>
    public sealed class RelayCommand : ICommand
    {
        #region Fields
        private readonly Action<object> execute;
        private readonly Predicate<object> canExecute;
        #endregion
        #region Constructors
        public RelayCommand(Action<object> execute)
            : this(execute, null)
        {
        }
        public RelayCommand(Action<object> execute, Predicate<object> canExecute)
        {
            if (execute == null)
            {
                throw new ArgumentNullException("execute");
            }
            this.execute = execute;
            this.canExecute = canExecute;
        }
        #endregion
        #region ICommand Members [DebuggerStepThrough]
        public bool CanExecute(object parameter)
        {
            return this.canExecute == null || this.canExecute(parameter);
        }
        public event EventHandler CanExecuteChanged
        {
            add
            {
                CommandManager.RequerySuggested += value;
            }
            remove
            {
                CommandManager.RequerySuggested -= value;
            }
        }
        public void Execute(object parameter)
        {
            this.execute(parameter);
        }
        #endregion
    }
}

Comments