Created
June 12, 2014 09:58
-
-
Save bronumski/ed76e342ee8e404709df to your computer and use it in GitHub Desktop.
WPF Relay command
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
| public class RelayCommand<T> : ICommand | |
| { | |
| private readonly Predicate<T> canExecute; | |
| private readonly Action<T> execute; | |
| public RelayCommand(Action<T> action) | |
| : this(x => true, action) | |
| { | |
| execute = action; | |
| } | |
| public RelayCommand(Predicate<T> canExecute, Action<T> execute) | |
| { | |
| this.canExecute = canExecute; | |
| this.execute = execute; | |
| } | |
| public event EventHandler CanExecuteChanged; | |
| public bool CanExecute(object parameter) | |
| { | |
| return canExecute((T)parameter); | |
| } | |
| public void UpdateCanExecuteState() | |
| { | |
| if (CanExecuteChanged != null) | |
| { | |
| CanExecuteChanged(this, new EventArgs()); | |
| } | |
| } | |
| public void Execute(object parameter) | |
| { | |
| execute((T)parameter); | |
| UpdateCanExecuteState(); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment