Created
February 3, 2016 19:43
-
-
Save rmnblm/2bccb88463ade0342cd6 to your computer and use it in GitHub Desktop.
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.Windows.Input; | |
/// <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 | |
{ | |
private readonly Action execute; | |
private readonly Func<bool> canExecute; | |
public event EventHandler CanExecuteChanged; | |
public RelayCommand(Action execute) | |
: this(execute, null) | |
{ | |
} | |
public RelayCommand(Action execute, | |
Func<bool> canExecute) | |
{ | |
this.execute = execute; | |
this.canExecute = canExecute; | |
} | |
public bool CanExecute(object parameter) | |
{ | |
if (canExecute == null) | |
{ | |
return true; | |
} | |
return canExecute(); | |
} | |
public void Execute(object parameter) | |
{ | |
execute(); | |
} | |
public void RaiseCanExecuteChanged() | |
{ | |
if (CanExecuteChanged != null) | |
{ | |
CanExecuteChanged(this, EventArgs.Empty); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment