using System; using System.Windows.Input; using System.Windows.Markup; namespace Samples.Commands { /// /// Basic implementation of the /// interface, which is also accessible as a markup /// extension. /// public abstract class CommandBase : MarkupExtension, ICommand where T : class, ICommand, new() { /// /// A singleton instance. /// private static T command; /// /// Gets a shared command instance. /// public override object ProvideValue(IServiceProvider serviceProvider) { if (command == null) command = new T(); return command; } /// /// Fires when changes occur that affect whether /// or not the command should execute. /// public event EventHandler CanExecuteChanged { add { CommandManager.RequerySuggested += value; } remove { CommandManager.RequerySuggested -= value; } } /// /// Defines the method to be called when the command is invoked. /// /// Data used by the command. /// If the command does not require data to be passed, /// this object can be set to null. /// public abstract void Execute(object parameter); /// /// Defines the method that determines whether the command /// can execute in its current state. /// /// /// This default implementation always returns true. /// /// Data used by the command. /// If the command does not require data to be passed, /// this object can be set to null. /// public virtual bool CanExecute(object parameter) { return true; } } }