59
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
1 using System;
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
2 using System.Windows.Input;
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
3
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
4 namespace SilverlightValidation.Commands
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
5 {
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
6 public class RelayCommand : ICommand
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
7 {
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
8 public event EventHandler CanExecuteChanged = delegate { };
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
9
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
10 readonly Action<object> _execute;
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
11 readonly Predicate<object> _canExecute;
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
12
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
13 public RelayCommand(Action<object> execute,
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
14 Predicate<object> canExecute = null)
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
15 {
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
16 if (execute == null) throw new ArgumentNullException("execute");
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
17
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
18 _execute = execute;
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
19 _canExecute = canExecute;
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
20 }
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
21
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
22
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
23 public void UpdateCanExecuteCommand()
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
24 {
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
25 CanExecuteChanged(this, new EventArgs());
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
26 }
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
27
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
28
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
29 public bool CanExecute(object parameter)
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
30 {
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
31 return _canExecute == null || _canExecute(parameter);
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
32 }
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
33
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
34
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
35 public void Execute(object parameter)
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
36 {
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
37 _execute(parameter);
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
38 }
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
39 }
|
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
diff
changeset
|
40 }
|