-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRelayCommand.cs
More file actions
35 lines (28 loc) · 1.11 KB
/
RelayCommand.cs
File metadata and controls
35 lines (28 loc) · 1.11 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
using System.Windows.Input;
namespace DeadDailyDose;
/// <summary>
/// Simple ICommand implementation for MVVM button binding.
/// </summary>
public class RelayCommand : ICommand
{
private readonly Action<object?> _execute;
private readonly Predicate<object?>? _canExecute;
/// <summary>Creates a command that can always execute.</summary>
public RelayCommand(Action<object?> execute) : this(execute, null) { }
/// <summary>Creates a command with optional canExecute predicate.</summary>
public RelayCommand(Action<object?> execute, Predicate<object?>? canExecute)
{
_execute = execute ?? throw new ArgumentNullException(nameof(execute));
_canExecute = canExecute;
}
/// <inheritdoc />
public bool CanExecute(object? parameter) => _canExecute == null || _canExecute(parameter);
/// <inheritdoc />
public void Execute(object? parameter) => _execute(parameter);
/// <inheritdoc />
public event EventHandler? CanExecuteChanged
{
add => CommandManager.RequerySuggested += value;
remove => CommandManager.RequerySuggested -= value;
}
}