1: public class Command : ICommand
2: { 3: private bool m_canExecuteCache;
4: private Action<object> m_executeAction;
5: private Func<object, bool> m_canExecute;
6:
7: /// <summary>
8: /// This event occurs when changes occur that
9: /// affect whether the command should execute.
10: /// </summary>
11: public event EventHandler CanExecuteChanged;
12:
13: /// <summary>
14: /// Initializes a new instance of <see cref="Command"/> class.
15: /// </summary>
16: /// <param name="canExecute">
17: /// Encapsulates a method that has one
18: /// parameter and returns a value of the type bool.
19: /// </param>
20: /// <param name="executeAction">
21: /// Encapsulates a method that takes a single
22: /// parameter and does not return a value.
23: /// </param>
24: public Command(Func<object, bool> canExecute, Action<object> executeAction)
25: { 26: m_canExecute = canExecute;
27: m_executeAction = executeAction;
28: }
29:
30: /// <summary>
31: /// This method determines whether the
32: /// command can execute in its current state.
33: /// </summary>
34: /// <param name="parameter">
35: /// Data used by the command. If the command does not
36: /// require data to be passed, this object can be set to null.
37: /// </param>
38: /// <returns>
39: /// true if this command can be executed; otherwise, false.
40: /// </returns>
41: public bool CanExecute(object parameter)
42: { 43: bool enable = m_canExecute(parameter);
44:
45: if (m_canExecuteCache != enable)
46: { 47: m_canExecuteCache = enable;
48:
49: if (CanExecuteChanged != null)
50: { 51: CanExecuteChanged(this, new EventArgs());
52: }
53: }
54:
55: return m_canExecuteCache;
56: }
57:
58: /// <summary>
59: /// This method will be called when the command is invoked.
60: /// </summary>
61: /// <param name="parameter">
62: /// Data used by the command. If the command does not
63: /// require data to be passed, this object can be set to null.
64: /// </param>
65: public void Execute(object parameter)
66: { 67: m_executeAction(parameter);
68: }
69: }