using System; using System.Windows.Input; namespace JD.Guinness.WPF.Module { public sealed class DelegateCommand : ICommand { #region Fields private readonly Action _execute; private readonly Func _canExecute; #endregion #region Initialisation public DelegateCommand(Action execute, Func canExecute) { _execute = execute ?? throw new ArgumentNullException(nameof(execute)); _canExecute = canExecute; } public DelegateCommand(Action execute) : this(execute, null) { } #endregion #region Methods public bool CanExecute() => _canExecute?.Invoke() ?? true; public void Execute() => _execute(); #endregion #region ICommand event EventHandler ICommand.CanExecuteChanged { add => CommandManager.RequerySuggested += value; remove => CommandManager.RequerySuggested -= value; } bool ICommand.CanExecute(object parameter) => CanExecute(); void ICommand.Execute(object parameter) => Execute(); #endregion } public sealed class DelegateCommand : ICommand { #region Fields private readonly Action _execute; private readonly Predicate _canExecute; private readonly bool _isClass; #endregion #region Initialisation public DelegateCommand(Action execute, Predicate canExecute) { _execute = execute ?? throw new ArgumentNullException(nameof(execute)); _canExecute = canExecute; _isClass = typeof(T).IsClass; } public DelegateCommand(Action execute) : this(execute, null) { } #endregion #region Methods public bool CanExecute(T parameter) => _canExecute?.Invoke(parameter) ?? true; public void Execute(T parameter) => _execute(parameter); #endregion #region ICommand event EventHandler ICommand.CanExecuteChanged { add => CommandManager.RequerySuggested += value; remove => CommandManager.RequerySuggested -= value; } bool ICommand.CanExecute(object parameter) { if (!(parameter is T) || (parameter == null && !_isClass)) { return false; } return CanExecute((T)parameter); } void ICommand.Execute(object parameter) => Execute((T)parameter); #endregion } }