-
-
Save jasondown/effba3451d3ae02290670d69bf911478 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Windows.Input; | |
namespace Jason.Down.Blog.MutliImageAddinDemo.Command | |
{ | |
/// <summary> | |
/// The <see cref="RelayCommand" /> simply invokes delegates for given command functionality. | |
/// <see cref="CanExecute" /> returns true by default. | |
/// </summary> | |
[Serializable] | |
public class RelayCommand : ICommand | |
{ | |
private readonly Func<bool> _canExecute; | |
private readonly Action _execute; | |
private readonly Action<Object> _executeParam; | |
/// <summary> | |
/// Initializes a new instance of the <see cref="RelayCommand" /> class. | |
/// This command can always execute (no predicate to control execution logic). | |
/// </summary> | |
/// <param name="execute">The execute.</param> | |
public RelayCommand(Action<Object> execute) | |
: this(execute, null) | |
{ | |
} | |
/// <summary> | |
/// Initializes a new instance of the <see cref="RelayCommand" /> class. | |
/// </summary> | |
/// <param name="execute">The execute command.</param> | |
/// <param name="canExecute">A predicate that controls whether or not the command can execute.</param> | |
/// <exception cref="System.ArgumentNullException">execute</exception> | |
public RelayCommand(Action<object> execute, Func<bool> canExecute) | |
{ | |
if (execute == null) | |
{ | |
throw new ArgumentNullException("execute"); | |
} | |
_executeParam = execute; | |
_canExecute = canExecute; | |
} | |
/// <summary> | |
/// Initializes a new instance of the <see cref="RelayCommand" /> class. | |
/// </summary> | |
/// <param name="execute">The execute command.</param> | |
public RelayCommand(Action execute) | |
: this(execute, null) | |
{ | |
} | |
/// <summary> | |
/// Initializes a new instance of the <see cref="RelayCommand" /> class. | |
/// </summary> | |
/// <param name="execute">The execute command.</param> | |
/// <param name="canExecute">A predicate that controls whether or not the command can execute.</param> | |
/// <exception cref="System.ArgumentNullException">execute</exception> | |
public RelayCommand(Action execute, Func<bool> canExecute) | |
{ | |
if (execute == null) | |
{ | |
throw new ArgumentNullException("execute"); | |
} | |
_execute = execute; | |
_canExecute = canExecute; | |
} | |
/// <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> | |
/// <c>true</c> if this command can be executed; otherwise, <c>false</c>. | |
/// </returns> | |
public bool CanExecute(object parameter) | |
{ | |
if (_canExecute != null) | |
{ | |
return _canExecute(); | |
} | |
return true; | |
} | |
/// <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) | |
{ | |
if (_execute != null) | |
{ | |
_execute(); | |
} | |
else if (_executeParam != null) | |
{ | |
_executeParam(parameter); | |
} | |
} | |
/// <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; } | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment