Rev | Line | |
---|
[11926] | 1 | using System; |
---|
| 2 | using System.Collections.Generic; |
---|
| 3 | using System.Linq; |
---|
| 4 | using System.Text; |
---|
| 5 | using System.Windows.Input; |
---|
| 6 | |
---|
| 7 | namespace Com.Piwigo.Wpf.Command |
---|
| 8 | { |
---|
| 9 | public class DelegateCommand : ICommand |
---|
| 10 | { |
---|
| 11 | private readonly Predicate<object> _canExecute; |
---|
| 12 | private readonly Action<object> _execute; |
---|
| 13 | private Boolean _isRunning; |
---|
| 14 | |
---|
| 15 | public event EventHandler CanExecuteChanged; |
---|
| 16 | |
---|
| 17 | public DelegateCommand(Action<object> execute) |
---|
| 18 | : this(execute, null) |
---|
| 19 | { |
---|
| 20 | } |
---|
| 21 | |
---|
| 22 | public DelegateCommand(Action<object> execute, |
---|
| 23 | Predicate<object> canExecute) |
---|
| 24 | { |
---|
| 25 | _execute = execute; |
---|
| 26 | _canExecute = canExecute; |
---|
| 27 | _isRunning = false; |
---|
| 28 | } |
---|
| 29 | |
---|
| 30 | public bool CanExecute(object parameter) |
---|
| 31 | { |
---|
| 32 | if ((_isRunning) || (_canExecute == null)) |
---|
| 33 | { |
---|
| 34 | return false; |
---|
| 35 | } |
---|
| 36 | |
---|
| 37 | return _canExecute(parameter); |
---|
| 38 | } |
---|
| 39 | |
---|
| 40 | public void Execute(object parameter) |
---|
| 41 | { |
---|
| 42 | _isRunning = true; |
---|
| 43 | RaiseCanExecuteChanged(); |
---|
| 44 | _execute(parameter); |
---|
| 45 | _isRunning = false; |
---|
| 46 | RaiseCanExecuteChanged(); |
---|
| 47 | } |
---|
| 48 | |
---|
| 49 | public void RaiseCanExecuteChanged() |
---|
| 50 | { |
---|
| 51 | if (CanExecuteChanged != null) |
---|
| 52 | { |
---|
| 53 | CanExecuteChanged(this, EventArgs.Empty); |
---|
| 54 | } |
---|
| 55 | } |
---|
| 56 | } |
---|
| 57 | } |
---|
Note: See
TracBrowser
for help on using the repository browser.