Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Windows.Input;
- public class RelayCommand<T> : ICommand {
- private readonly Action<T> _execute = null;
- private readonly Func<T, bool> _canExecute = null;
- public RelayCommand(Action<T> execute, Func<T, bool> canExecute = null) {
- _execute = execute ?? throw new ArgumentNullException(nameof(execute));
- _canExecute = canExecute ?? (_ => true);
- }
- public event EventHandler CanExecuteChanged {
- add => CommandManager.RequerySuggested += value;
- remove => CommandManager.RequerySuggested -= value;
- }
- public bool CanExecute(object parameter) => _canExecute((T)parameter);
- public void Execute(object parameter) => _execute((T)parameter);
- }
- public class RelayCommand : RelayCommand<object> {
- public RelayCommand(Action execute)
- : base(_ => execute()) { }
- public RelayCommand(Action execute, Func<bool> canExecute)
- : base(_ => execute(), _ => canExecute()) { }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement