using System; using System.Collections.Generic; using System.Linq; using System.Linq.Expressions; using System.Text; using System.Threading.Tasks; namespace AIStudio.Wpf.DiagramDesigner.Additionals.Commands { /// /// An whose delegates do not take any parameters for and . /// /// /// public class DelegateCommand : DelegateCommandBase { Action _executeMethod; Func _canExecuteMethod; /// /// Creates a new instance of with the to invoke on execution. /// /// The to invoke when is called. public DelegateCommand(Action executeMethod) : this(executeMethod, () => true) { } /// /// Creates a new instance of with the to invoke on execution /// and a to query for determining if the command can execute. /// /// The to invoke when is called. /// The to invoke when is called public DelegateCommand(Action executeMethod, Func canExecuteMethod) : base() { if (executeMethod == null || canExecuteMethod == null) throw new ArgumentNullException(nameof(executeMethod), "DelegateCommandDelegatesCannotBeNull"); _executeMethod = executeMethod; _canExecuteMethod = canExecuteMethod; } /// /// Executes the command. /// public void Execute() { _executeMethod(); } /// /// Determines if the command can be executed. /// /// Returns if the command can execute,otherwise returns . public bool CanExecute() { return _canExecuteMethod(); } /// /// Handle the internal invocation of /// /// Command Parameter protected override void Execute(object parameter) { Execute(); } /// /// Handle the internal invocation of /// /// /// if the Command Can Execute, otherwise protected override bool CanExecute(object parameter) { return CanExecute(); } /// /// Observes a property that implements INotifyPropertyChanged, and automatically calls DelegateCommandBase.RaiseCanExecuteChanged on property changed notifications. /// /// The object type containing the property specified in the expression. /// The property expression. Example: ObservesProperty(() => PropertyName). /// The current instance of DelegateCommand public DelegateCommand ObservesProperty(Expression> propertyExpression) { ObservesPropertyInternal(propertyExpression); return this; } /// /// Observes a property that is used to determine if this command can execute, and if it implements INotifyPropertyChanged it will automatically call DelegateCommandBase.RaiseCanExecuteChanged on property changed notifications. /// /// The property expression. Example: ObservesCanExecute(() => PropertyName). /// The current instance of DelegateCommand public DelegateCommand ObservesCanExecute(Expression> canExecuteExpression) { _canExecuteMethod = canExecuteExpression.Compile(); ObservesPropertyInternal(canExecuteExpression); return this; } } }