Last active
February 23, 2021 03:34
-
-
Save SnowyPainter/dc250d3373dc2ee9d664ffd426fbdb17 to your computer and use it in GitHub Desktop.
xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
This file contains hidden or 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.Collections.Generic; | |
using System.Text; | |
using System.Windows.Input; | |
namespace a | |
{ | |
public class RelayCommand<T> : ICommand | |
{ | |
#region Fields | |
readonly Action<T> _execute = null; | |
readonly Predicate<T> _canExecute = null; | |
#endregion | |
#region Constructors | |
/// <summary> | |
/// Initializes a new instance of <see cref="DelegateCommand{T}"/>. | |
/// </summary> | |
/// <param name="execute">Delegate to execute when Execute is called on the command. This can be null to just hook up a CanExecute delegate.</param> | |
/// <remarks><seealso cref="CanExecute"/> will always return true.</remarks> | |
public RelayCommand(Action<T> execute) | |
: this(execute, null) | |
{ | |
} | |
/// <summary> | |
/// Creates a new command. | |
/// </summary> | |
/// <param name="execute">The execution logic.</param> | |
/// <param name="canExecute">The execution status logic.</param> | |
public RelayCommand(Action<T> execute, Predicate<T> canExecute) | |
{ | |
if (execute == null) | |
throw new ArgumentNullException("execute"); | |
_execute = execute; | |
_canExecute = canExecute; | |
} | |
#endregion | |
#region ICommand Members | |
///<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 _canExecute == null ? true : _canExecute((T)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; } | |
} | |
///<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 <see langword="null" />.</param> | |
public void Execute(object parameter) | |
{ | |
_execute((T)parameter); | |
} | |
#endregion | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment