// Developed by doiTTeam => devdoiTTeam@gmail.com using System; using System.Windows.Input; namespace DraggAnimatedPanelExample { /// /// An whose delegates can be attached for and . /// It also implements the interface, which is useful when registering this command in a that monitors command's activity. /// /// Parameter type. /// /// The constructor deliberately prevent the use of value types. /// Because ICommand takes an object, having a value type for T would cause unexpected behavior when CanExecute(null) is called during XAML initialization for command bindings. /// Using default(T) was considered and rejected as a solution because the implementor would not be able to distinguish between a valid and defaulted values. /// /// Instead, callers should support a value type by using a nullable value type and checking the HasValue property before using the Value property. /// /// /// public MyClass() /// { /// this.submitCommand = new DelegateCommand<int?>(this.Submit, this.CanSubmit); /// } /// /// private bool CanSubmit(int? customerId) /// { /// return (customerId.HasValue && customers.Contains(customerId.Value)); /// } /// /// /// public class DelegateCommand : DelegateCommandBase { /// /// Initializes a new instance of . /// /// Delegate to execute when Execute is called on the command. This can be null to just hook up a CanExecute delegate. /// /// will always return true. /// public DelegateCommand(Action executeMethod) : this(executeMethod, (o) => true) { } /// /// Initializes a new instance of . /// /// Delegate to execute when Execute is called on the command. This can be null to just hook up a CanExecute delegate. /// Delegate to execute when CanExecute is called on the command. This can be null. /// When both and ar . public DelegateCommand(Action executeMethod, Func canExecuteMethod) : base((o) => executeMethod((T) o), (o) => canExecuteMethod((T) o)) { if (executeMethod == null || canExecuteMethod == null) throw new ArgumentNullException("executeMethod"); } /// /// Determines if the command can execute by invoked the provided during construction. /// ///Data used by the command to determine if it can execute. /// /// if this command can be executed; otherwise, . /// public bool CanExecute(T parameter) { return base.CanExecute(parameter); } /// /// Executes the command and invokes the provided during construction. /// ///Data used by the command. public void Execute(T parameter) { base.Execute(parameter); } } /// /// An whose delegates do not take any parameters for and . /// /// /// public class DelegateCommand : DelegateCommandBase { /// /// 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((o) => executeMethod(), (o) => canExecuteMethod()) { if (executeMethod == null || canExecuteMethod == null) throw new ArgumentNullException("executeMethod"); } /// /// Executes the command. /// public void Execute() { Execute(null); } /// /// Determines if the command can be executed. /// /// Returns if the command can execute,otherwise returns . public bool CanExecute() { return CanExecute(null); } } }