-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathRelayCommand.cs
More file actions
72 lines (63 loc) · 2.09 KB
/
RelayCommand.cs
File metadata and controls
72 lines (63 loc) · 2.09 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
using System.Windows;
using System.Windows.Input;
namespace CADPythonShell
{
/// <summary>
/// A general relay command that takes its parameter as an object
/// </summary>
public class RelayCommand : ICommand
{
private readonly Predicate<object> m_canExecute;
private readonly Action<object> m_execute;
private readonly Action _act;
public RelayCommand(Action act)
{
_act = act;
}
public RelayCommand(Action<object> execute, Predicate<object> canExecute = null)
{
if (execute == null)
{
throw new ArgumentNullException("Execute");
}
m_execute = execute;
m_canExecute = canExecute;
}
// Evaluate the command if it is valid to execute
public bool CanExecute(object parameter = null)
{
if (parameter == null || m_canExecute == null) return true;
else return m_canExecute(parameter);
}
// Main execute method
public void Execute(object parameter = null)
{
if (_act != null) _act();
else m_execute(parameter);
}
// In WPF CommandManager is a pre-defined class that take charge of observing the user interface
// and calls the CanExecute method when it deems it necessary
public event EventHandler CanExecuteChanged
{
add => CommandManager.RequerySuggested += value;
remove => CommandManager.RequerySuggested -= value;
}
public class CloseCommand : ICommand
{
public bool CanExecute(object parameter)
{
return true;
}
public event EventHandler CanExecuteChanged
{
add => CommandManager.RequerySuggested += value;
remove => CommandManager.RequerySuggested -= value;
}
public void Execute(object parameter)
{
Window myWin = parameter as Window;
myWin.Close();
}
}
}
}