Created
December 7, 2016 17:28
-
-
Save ginomessmer/9763d32243f8d13963cfe328ae7966fd to your computer and use it in GitHub Desktop.
DelegateCommand for MVVM
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
public class DelegateCommand<T> : ICommand where T : class | |
{ | |
private readonly Predicate<T> _canExecute; | |
private readonly Action<T> _execute; | |
public DelegateCommand(Action<T> execute) | |
: this(execute, null) | |
{ | |
} | |
public DelegateCommand(Action<T> execute, Predicate<T> canExecute) | |
{ | |
_execute = execute; | |
_canExecute = canExecute; | |
} | |
public bool CanExecute(object parameter) | |
{ | |
if (_canExecute == null) | |
return true; | |
return _canExecute((T)parameter); | |
} | |
public void Execute(object parameter) | |
{ | |
_execute((T)parameter); | |
} | |
public event EventHandler CanExecuteChanged; | |
public void RaiseCanExecuteChanged() | |
{ | |
CanExecuteChanged?.Invoke(this, EventArgs.Empty); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment