using System; using System.Windows.Input; namespace WowUp.WPF.Utilities { public class Command : ICommand { readonly Func _canExecute; readonly Action _execute; readonly WeakEventManager _weakEventManager = new WeakEventManager(); public Command(Action execute) { if (execute == null) throw new ArgumentNullException(nameof(execute)); _execute = execute; } public Command(Action execute) : this(o => execute()) { if (execute == null) throw new ArgumentNullException(nameof(execute)); } public Command(Action execute, Func canExecute) : this(execute) { if (canExecute == null) throw new ArgumentNullException(nameof(canExecute)); _canExecute = canExecute; } public Command(Action execute, Func canExecute) : this(o => execute(), o => canExecute()) { if (execute == null) throw new ArgumentNullException(nameof(execute)); if (canExecute == null) throw new ArgumentNullException(nameof(canExecute)); } public bool CanExecute(object parameter) { if (_canExecute != null) return _canExecute(parameter); return true; } public event EventHandler CanExecuteChanged { add { _weakEventManager.AddEventHandler(value); } remove { _weakEventManager.RemoveEventHandler(value); } } public void Execute(object parameter) { _execute(parameter); } public void ChangeCanExecute() { _weakEventManager.HandleEvent(this, EventArgs.Empty, nameof(CanExecuteChanged)); } } }