-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathRelayCommand.cs
36 lines (28 loc) · 1.2 KB
/
RelayCommand.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
using System;
using System.Diagnostics;
using System.Windows.Input;
namespace FFBitrateViewer
{
// Source: https://docs.microsoft.com/en-us/archive/msdn-magazine/2009/february/patterns-wpf-apps-with-the-model-view-viewmodel-design-pattern#id0090030
public class RelayCommand : ICommand
{
readonly Action<object?> _execute;
readonly Predicate<object?>? _canExecute;
public RelayCommand(Action<object?> execute) : this(execute, null) { }
public RelayCommand(Action<object?> execute, Predicate<object?>? canExecute)
{
if (execute == null) throw new ArgumentNullException("execute");
_execute = execute;
_canExecute = canExecute;
}
[DebuggerStepThrough]
public bool CanExecute(object? parameter) => _canExecute?.Invoke(parameter) ?? true;
public event EventHandler? CanExecuteChanged
{
add { CommandManager.RequerySuggested += value; }
remove { CommandManager.RequerySuggested -= value; }
}
public void RaiseCanExecuteChanged() => CommandManager.InvalidateRequerySuggested();
public void Execute(object? parameter) => _execute(parameter);
}
}