-
Notifications
You must be signed in to change notification settings - Fork 1
/
CommandHook.cs
71 lines (62 loc) · 2.5 KB
/
CommandHook.cs
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
using System;
using System.ComponentModel;
using System.Windows.Input;
namespace Rot13
{
/// <summary>
/// Implements ICommand. Based to some extent on the Prism DelegateCommand class but without
/// quite so many bells & whistles
/// </summary>
public class CommandHook : ICommand
{
Func<object, bool> canExecute;
Action<object> action;
public event EventHandler CanExecuteChanged;
// Constructors
public CommandHook(Action<object> action)
: this(action, (_) => true, null)
{ }
public CommandHook(Action<object> action, Func<object, bool> canExecute)
: this(action, canExecute, null)
{ }
// Action is required. CanExecute defaults to true. Parent is optional
public CommandHook(Action<object> action, Func<object, bool> canExecute, INotifyPropertyChanged parent)
{
this.canExecute = canExecute;
this.action = action;
if (parent != null)
parent.PropertyChanged += Parent_PropertyChanged;
}
/// <summary>
/// If CanExecute(p) returns true the UI element attached to it will be enabled
/// </summary>
/// <param name="parameter"></param>
/// <returns></returns>
public bool CanExecute(object parameter)
=> canExecute(parameter);
/// <summary>
/// Execute the command.
/// </summary>
/// <param name="parameter"></param>
public void Execute(object parameter)
=> action(parameter);
/// <summary>
/// Call this when the state of a commandable object has changed but has not
/// necessarily updated a notifiable property (or if the object does not
/// implement INotifyPropertyChanged)
/// </summary>
public void SuggestCanExecuteChanged()
=> OnCanExecuteChanged();
// Ping the WPF runtime to check if the changed parameter should also result in a
// change to the executable status of this command.
// Might not want to do this *every time* a property on the parent changes, but
// for small use cases (or small containers!) it's probably ok
private void Parent_PropertyChanged(object sender, PropertyChangedEventArgs e)
=> OnCanExecuteChanged();
private void OnCanExecuteChanged()
{
if (CanExecuteChanged != null)
CanExecuteChanged(this, new EventArgs());
}
}
}