// Developed by doiTTeam => devdoiTTeam@gmail.com using System; using System.Diagnostics.CodeAnalysis; using System.Windows.Input; namespace DraggAnimatedPanelExample { /// /// An whose delegates can be attached for and . /// public abstract class DelegateCommandBase : ICommand { private readonly Func canExecuteMethod; private readonly Action executeMethod; /// /// Createse a new instance of a , specifying both the execute action and the can execute function. /// /// The to execute when is invoked. /// The to invoked when is invoked. protected DelegateCommandBase(Action executeMethod, Func canExecuteMethod) { if (executeMethod == null || canExecuteMethod == null) throw new ArgumentNullException("executeMethod"); this.executeMethod = executeMethod; this.canExecuteMethod = canExecuteMethod; } #region ICommand Members void ICommand.Execute(object parameter) { Execute(parameter); } bool ICommand.CanExecute(object parameter) { return CanExecute(parameter); } /// /// Occurs when changes occur that affect whether or not the command should execute. /// public event EventHandler CanExecuteChanged; #endregion /// /// Raises on the UI thread so every /// command invoker can requery to check if the /// can execute. /// protected virtual void OnCanExecuteChanged() { var handlers = CanExecuteChanged; if (handlers != null) { handlers(this, EventArgs.Empty); } } /// /// Raises on the UI thread so every command invoker /// can requery to check if the command can execute. /// /// Note that this will trigger the execution of once for each invoker. /// /// [SuppressMessage("Microsoft.Design", "CA1030:UseEventsWhereAppropriate")] public void RaiseCanExecuteChanged() { OnCanExecuteChanged(); } /// /// Executes the command with the provided parameter by invoking the supplied during construction. /// /// protected void Execute(object parameter) { executeMethod(parameter); } /// /// Determines if the command can execute with the provided parameter by invoing the supplied during construction. /// /// The parameter to use when determining if this command can execute. /// Returns if the command can execute. otherwise. protected bool CanExecute(object parameter) { return canExecuteMethod == null || canExecuteMethod(parameter); } } }