source: extensions/PiwigoLib/PiwigoWpf/Command/DelegateCommand.cs @ 11926

Last change on this file since 11926 was 11926, checked in by bayral, 13 years ago

New step for piwigowpf

File size: 1.4 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using System.Windows.Input;
6
7namespace 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.