-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRelayCommand.cs
More file actions
30 lines (29 loc) · 1.1 KB
/
RelayCommand.cs
File metadata and controls
30 lines (29 loc) · 1.1 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
using System;
using System.Windows.Input;
namespace CommonWPFTools
{
public class RelayCommand : ICommand
{
#region Fields
readonly Action<object> _execute;
readonly Predicate<object> _canExecute;
#endregion // Fields
#region Constructors
public RelayCommand(Action<object> execute) : this(execute, null) { }
public RelayCommand(Action<object> execute, Predicate<object> canExecute)
{
_execute = execute ?? throw new ArgumentNullException(nameof(execute)); _canExecute = canExecute;
}
#endregion // Constructors
#region ICommand Members
[System.Diagnostics.DebuggerStepThrough]
public bool CanExecute(object parameter) => _canExecute == null ? true : _canExecute(parameter);
public event EventHandler CanExecuteChanged
{
add { CommandManager.RequerySuggested += value; }
remove { CommandManager.RequerySuggested -= value; }
}
public void Execute(object parameter) => _execute(parameter);
#endregion // ICommand Members
}
}