123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Windows.Input;
- namespace Aitex.Core.UI.MVVM
- {
- public class DelegateCommand<T> : ICommand
- {
- readonly Action<T> _execute;
- readonly Predicate<T> _canExecute;
- public event EventHandler CanExecuteChanged;
- public DelegateCommand(Action<T> execute)
- : this(execute, null)
- {
- }
- public DelegateCommand(Action<T> execute, Predicate<T> canExecute)
- {
- if (execute == null)
- throw new ArgumentNullException("execute");
- _execute = execute;
- _canExecute = canExecute;
- }
- public bool CanExecute(object parameter)
- {
- return _canExecute == null ? true : _canExecute((T)parameter);
- }
-
- // The CanExecuteChanged is automatically registered by command binding, we can assume that it has some execution logic
- // to update the button's enabled\disabled state(though we cannot see). So raises this event will cause the button's state be updated.
- public void RaiseCanExecuteChanged()
- {
- if (CanExecuteChanged != null)
- CanExecuteChanged(this, EventArgs.Empty);
- }
- public void Execute(object parameter)
- {
- _execute((T)parameter);
- }
- }
- public class DelegateCommand : ICommand
- {
- private readonly Action _execute;
- private readonly Func<bool> _canExecute;
- public DelegateCommand(Action execute) : this(execute, null) { }
- public DelegateCommand(Action execute, Func<bool> canExecute)
- {
- _execute = execute ?? throw new ArgumentNullException(nameof(execute));
- _canExecute = canExecute;
- }
- public void Execute(object parameter)
- {
- _execute();
- }
- public bool CanExecute(object parameter)
- {
- if (_canExecute == null) return true;
- return _canExecute();
- }
- public event EventHandler CanExecuteChanged
- {
- add => CommandManager.RequerySuggested += value;
- remove => CommandManager.RequerySuggested -= value;
- }
- }
- }
|