-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDelegateCommand.cs
More file actions
80 lines (72 loc) · 2.54 KB
/
Copy pathDelegateCommand.cs
File metadata and controls
80 lines (72 loc) · 2.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
using System;
using System.Windows.Input;
using System.Diagnostics;
namespace NumediaStack
{
/// <summary>
/// Simple implementation of the DelegateCommand (aka RelayCommand) pattern.
/// </summary>
internal class DelegateCommand : ICommand
{
/// <summary>
/// Stores a reference to the CanExecute Func.
/// </summary>
private readonly Func<object, bool> _canExecute;
/// <summary>
/// Stores a reference to the Execute Action.
/// </summary>
private readonly Action<object> _execute;
/// <summary>
/// Initializes a new instance of the DelegateCommand class.
/// </summary>
/// <param name="execute">Action to invoke when Execute is called.</param>
public DelegateCommand(Action<object> execute)
: this(param => true, execute)
{
}
/// <summary>
/// Initializes a new instance of the DelegateCommand class.
/// </summary>
/// <param name="canExecute">Func to invoke when CanExecute is called.</param>
/// <param name="execute">Action to invoke when Execute is called.</param>
public DelegateCommand(Func<object, bool> canExecute, Action<object> execute)
{
Debug.Assert(canExecute != null, "canExecute must not be null.");
Debug.Assert(execute != null, "execute must not be null.");
_canExecute = canExecute;
_execute = execute;
}
/// <summary>
/// Determines if the command can execute.
/// </summary>
/// <param name="parameter">Data used by the command.</param>
/// <returns>True if the command can execute.</returns>
public bool CanExecute(object parameter)
{
return _canExecute(parameter);
}
/// <summary>
/// Executes the command.
/// </summary>
/// <param name="parameter">Data used by the command.</param>
public void Execute(object parameter)
{
_execute(parameter);
}
/// <summary>
/// Invoked when changes to the execution state of the command occur.
/// </summary>
public event EventHandler CanExecuteChanged;
/// <summary>
/// Invokes the CanExecuteChanged event.
/// </summary>
public void OnCanExecuteChanged()
{
EventHandler handler = CanExecuteChanged;
if (null != handler)
{
handler(this, EventArgs.Empty);
}
}
}
}