using System; using System.Diagnostics; using System.Windows.Input; namespace Palmmedia.WpfGraph.Common { /// <summary> /// A command whose sole purpose is to relay its functionality to other /// objects by invoking delegates. The default return value for the CanExecute /// method is 'true'. /// </summary> public class RelayCommand : ICommand { /// <summary> /// The action which is executed when command is executed. /// </summary> private readonly Action<object> execute; /// <summary> /// Determines whether the command can be executed. /// </summary> private readonly Predicate<object> canExecute; /// <summary> /// Initializes a new instance of the <see cref="RelayCommand"/> class. /// This instance can always execute. /// </summary> /// <param name="execute">The action which is executed when command is executed.</param> public RelayCommand(Action<object> execute) : this(execute, null) { } /// <summary> /// Initializes a new instance of the <see cref="RelayCommand"/> class. /// This instance can always execute. /// </summary> /// <param name="execute">The action which is executed when command is executed.</param> /// <param name="canExecute">Determinates whether the command can be executed.</param> public RelayCommand(Action<object> execute, Predicate<object> canExecute) { if (execute == null) { throw new ArgumentNullException("execute"); } this.execute = execute; this.canExecute = canExecute; } #region ICommand Members /// <summary> /// Occurs when changes occur that affect whether or not the command should execute. /// </summary> public event EventHandler CanExecuteChanged { add { CommandManager.RequerySuggested += value; } remove { CommandManager.RequerySuggested -= value; } } /// <summary> /// Defines the method that determines whether the command can execute in its current state. /// </summary> /// <param name="parameter">Data used by the command. If the command does not require data to be passed, this object can be set to null.</param> /// <returns> /// true if this command can be executed; otherwise, false. /// </returns> public bool CanExecute(object parameter) { return this.canExecute == null ? true : this.canExecute(parameter); } /// <summary> /// Defines the method to be called when the command is invoked. /// </summary> /// <param name="parameter">Data used by the command. If the command does not require data to be passed, this object can be set to null.</param> public void Execute(object parameter) { this.execute(parameter); } #endregion } }
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.
This article, along with any associated source code and files, is licensed under The Apache License, Version 2.0