SHOW:
|
|
- or go back to the newest paste.
| 1 | - | public class RelayCommand<T> : ICommand |
| 1 | + | using System; |
| 2 | - | {
|
| 2 | + | using System.Windows.Input; |
| 3 | - | private readonly Action<T> _execute = null; |
| 3 | + | |
| 4 | - | private readonly Func<T, bool> _canExecute = null; |
| 4 | + | public class RelayCommand<T> : ICommand {
|
| 5 | private readonly Action<T> _execute = null; | |
| 6 | - | public RelayCommand(Action<T> execute, Func<T, bool> canExecute = null) |
| 6 | + | private readonly Func<T, bool> _canExecute = null; |
| 7 | - | {
|
| 7 | + | |
| 8 | - | _execute = execute ?? throw new ArgumentNullException(nameof(execute)); |
| 8 | + | public RelayCommand(Action<T> execute, Func<T, bool> canExecute = null) {
|
| 9 | - | _canExecute = canExecute ?? (_ => true); |
| 9 | + | _execute = execute ?? throw new ArgumentNullException(nameof(execute)); |
| 10 | - | } |
| 10 | + | _canExecute = canExecute ?? (_ => true); |
| 11 | } | |
| 12 | - | public event EventHandler CanExecuteChanged |
| 12 | + | |
| 13 | - | {
|
| 13 | + | public event EventHandler CanExecuteChanged {
|
| 14 | - | add => CommandManager.RequerySuggested += value; |
| 14 | + | add => CommandManager.RequerySuggested += value; |
| 15 | - | remove => CommandManager.RequerySuggested -= value; |
| 15 | + | remove => CommandManager.RequerySuggested -= value; |
| 16 | - | } |
| 16 | + | |
| 17 | ||
| 18 | - | public bool CanExecute(object parameter) => _canExecute((T)parameter); |
| 18 | + | public bool CanExecute(object parameter) => _canExecute((T)parameter); |
| 19 | ||
| 20 | - | public void Execute(object parameter) => _execute((T)parameter); |
| 20 | + | public void Execute(object parameter) => _execute((T)parameter); |
| 21 | } | |
| 22 | ||
| 23 | - | public class RelayCommand : RelayCommand<object> |
| 23 | + | public class RelayCommand : RelayCommand<object> {
|
| 24 | - | {
|
| 24 | + | public RelayCommand(Action execute) |
| 25 | - | public RelayCommand(Action execute) |
| 25 | + | : base(_ => execute()) { }
|
| 26 | - | : base(_ => execute()) { }
|
| 26 | + | |
| 27 | public RelayCommand(Action execute, Func<bool> canExecute) | |
| 28 | - | public RelayCommand(Action execute, Func<bool> canExecute) |
| 28 | + | : base(_ => execute(), _ => canExecute()) { }
|
| 29 | - | : base(_ => execute(), _ => canExecute()) { }
|
| 29 | + | } |
| 30 | - | } |
| 30 | + |