-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCommand.cs
More file actions
168 lines (144 loc) · 6.03 KB
/
Copy pathCommand.cs
File metadata and controls
168 lines (144 loc) · 6.03 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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
using System;
using System.Windows.Input;
namespace Sharp.Utils.Wpf
{
/// <summary>
/// A basic implementation of the ICommand interface where the command parameter is ignored.
/// </summary>
public class Command : ICommand
{
#region Static Methods
/// <summary>
/// Creates a new <see cref="Command"/> instance.
/// </summary>
/// <param name="execute">The action to perform when the command is executed.</param>
/// <param name="canExecute">A predicate that determines whether the command is currently able to execute.</param>
/// <returns>A <see cref="Command"/>.</returns>
public Command Create(Action execute, Func<bool> canExecute = null)
{
return new Command(execute, canExecute);
}
/// <summary>
/// Creates a new <see cref="Command{T}"/> instance.
/// </summary>
/// <param name="execute">The action to perform when the command is executed.</param>
/// <param name="canExecute">A predicate that determines whether the command is currently able to execute.</param>
/// <returns>A <see cref="Command{T}"/>.</returns>
public Command<T> Create<T>(Action<T> execute, Func<T, bool> canExecute = null)
{
return new Command<T>(execute, canExecute);
}
#endregion
private readonly Action execute;
private readonly Func<bool> canExecute;
/// <summary>
/// Creates a new <see cref="Command"/> instance.
/// </summary>
/// <param name="execute">The action to perform when the command is executed.</param>
public Command(Action execute)
: this(execute, null)
{ }
/// <summary>
/// Creates a new <see cref="Command"/> instance.
/// </summary>
/// <param name="execute">The action to perform when the command is executed.</param>
/// <param name="canExecute">A predicate that determines whether the command is currently able to execute.</param>
public Command(Action execute, Func<bool> canExecute)
{
this.execute = execute;
this.canExecute = canExecute;
}
#region ICommand Members
/// <summary>
/// Determines whether the command can execute in its current state.
/// </summary>
/// <param name="parameter">Data used by the command. This parameter is ignored by the <see cref="Command"/> class.</param>
/// <returns><value>true</value> if this command can be executed; otherwise, <value>false</value>.</returns>
public bool CanExecute(object parameter)
{
return canExecute == null || canExecute();
}
/// <summary>
/// Occurs when changes occur that affect whether or not the command should execute.
/// </summary>
public event EventHandler CanExecuteChanged
{
add { CommandManager.RequerySuggested += value; }
remove { CommandManager.RequerySuggested -= value; }
}
/// <summary>
/// Invokes the command.
/// </summary>
/// <param name="parameter">Data used by the command. This parameter is ignored by the <see cref="Command"/> class.</param>
public void Execute(object parameter)
{
if (execute != null)
execute();
}
#endregion
}
/// <summary>
/// A basic implementation of the ICommand interface that accepts a typed parameter.
/// </summary>
public class Command<T> : ICommand
{
private readonly Action<T> execute;
private readonly Func<T, bool> canExecute;
/// <summary>
/// Creates a new <see cref="Command{T}"/> instance.
/// </summary>
/// <param name="execute">The action to perform when the command is executed.</param>
public Command(Action<T> execute)
: this(execute, null)
{ }
/// <summary>
/// Creates a new <see cref="Command{T}"/> instance.
/// </summary>
/// <param name="execute">The action to perform when the command is executed.</param>
/// <param name="canExecute">A predicate that determines whether the command is currently able to execute.</param>
public Command(Action<T> execute, Func<T, bool> canExecute)
{
this.execute = execute;
this.canExecute = canExecute;
}
#region ICommand Members
/// <summary>
/// Determines whether the command can execute in its current state.
/// </summary>
/// <param name="parameter">Data used by the command.</param>
/// <returns><value>true</value> if this command can be executed; otherwise, <value>false</value>.</returns>
public bool CanExecute(object parameter)
{
ValidateParameterType(parameter);
return (canExecute != null) ? canExecute((T)parameter) : true;
}
/// <summary>
/// Occurs when changes occur that affect whether or not the command should execute.
/// </summary>
public event EventHandler CanExecuteChanged
{
add { CommandManager.RequerySuggested += value; }
remove { CommandManager.RequerySuggested -= value; }
}
/// <summary>
/// Invokes the command.
/// </summary>
/// <param name="parameter">Data used by the command.</param>
public void Execute(object parameter)
{
ValidateParameterType(parameter);
if (execute != null)
execute((T)parameter);
}
#endregion
private static void ValidateParameterType(object parameter)
{
if (parameter is T)
return;
var type = typeof(T);
if (parameter == null && (type.IsClass || (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Nullable<>))))
return;
throw new ArgumentException("Command parameter must be of type " + type.FullName);
}
}
}