-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRelayCommand.cs
More file actions
38 lines (32 loc) · 1.23 KB
/
RelayCommand.cs
File metadata and controls
38 lines (32 loc) · 1.23 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
using System.Windows.Input;
namespace Ventuz
{
public class RelayCommand : ICommand
{
private readonly Action<object?> _execute;
private readonly Predicate<object?>? _canExecute;
public RelayCommand(Action<object?> execute, Predicate<object?>? canExecute = null)
{
_execute = execute ?? throw new ArgumentNullException(nameof(execute));
_canExecute = canExecute;
}
public bool CanExecute(object? parameter) => _canExecute == null || _canExecute(parameter);
public void Execute(object? parameter) => _execute(parameter);
public event EventHandler? CanExecuteChanged
{
add => CommandManager.RequerySuggested += value;
remove => CommandManager.RequerySuggested -= value;
}
}
public static class RelayCommandExtensions
{
public static void CanExecuteChangedInvoke(this RelayCommand command)
{
var handler = typeof(RelayCommand).GetField("_canExecuteChanged", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
if (handler != null)
{
CommandManager.InvalidateRequerySuggested();
}
}
}
}