-
Shortcut
-
clsDC
-
Description
-
DelegateCommand class
-
Language
-
csharp
-
Types
-
Expansion
-
Author
-
Fons Sonnemans
-
Upload on
-
15-4-2015 09:43:05
-
Downloads
-
2193
public class DelegateCommand : DelegateCommand<object> {
public DelegateCommand(Action executeAction) : base(o => executeAction()) {
}
public DelegateCommand(Action executeAction, Func<bool> canExecute)
: base(o => executeAction(), o => canExecute()) {
}
}
public class DelegateCommand<T> : ICommand {
public event EventHandler CanExecuteChanged;
private Predicate<T> _canExecute = null;
private Action<T> _executeAction = null;
public DelegateCommand(Action<T> executeAction) {
_executeAction = executeAction;
}
public DelegateCommand(Action<T> executeAction, Predicate<T> canExecute)
: this(executeAction) {
_canExecute = canExecute;
}
public bool CanExecute(T parameter) {
if (_canExecute != null) {
return _canExecute(parameter);
}
return true;
}
public void Execute(T parameter) {
if (_executeAction != null)
_executeAction(parameter);
if (CanExecuteChanged != null)
CanExecuteChanged(this, EventArgs.Empty);
}
public void RaiseCanExecuteChanged() {
OnCanExecuteChanged();
}
protected virtual void OnCanExecuteChanged() {
EventHandler handler = CanExecuteChanged;
if (handler != null) {
handler(this, EventArgs.Empty);
}
}
#region ICommand Members
bool ICommand.CanExecute(object parameter) {
return this.CanExecute((T)(parameter ?? default(T)));
}
void ICommand.Execute(object parameter) {
this.Execute((T)parameter);
}
#endregion
}