-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRelayCommand.cs
More file actions
71 lines (56 loc) · 1.8 KB
/
RelayCommand.cs
File metadata and controls
71 lines (56 loc) · 1.8 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
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
// Copyright (c) 2015 WildCardJoker
// Licensed under the MIT License: http://opensource.org/licenses/MIT
// Created: 2015-07-14
// Last Modified: 2015-07-25-8:28 PM
#region Using Directives
using System;
using System.Diagnostics;
using System.Windows.Input;
#endregion
namespace ContactViewModel
{
/// <summary>
/// A command whose sole purpose is to relay its functionality to other
/// objects by invoking delegates. The default return value for the
/// CanExecute method is 'true'.
/// </summary>
public class RelayCommand : ICommand
{
#region Fields
private readonly Predicate<object> _canExecute;
private readonly Action<object> _execute;
#endregion
#region Constructors
/// <summary>
/// Creates a new command.
/// </summary>
/// <param name="execute">The execution logic.</param>
/// <param name="canExecute">The execution status logic.</param>
public RelayCommand(Action<object> execute, Predicate<object> canExecute = null)
{
if (execute == null)
{
throw new ArgumentNullException(nameof(execute));
}
_execute = execute;
_canExecute = canExecute;
}
#endregion
#region ICommand Members
[DebuggerStepThrough]
public bool CanExecute(object parameters)
{
return _canExecute == null || _canExecute(parameters);
}
public event EventHandler CanExecuteChanged
{
add { CommandManager.RequerySuggested += value; }
remove { CommandManager.RequerySuggested -= value; }
}
public void Execute(object parameters)
{
_execute(parameters);
}
#endregion
}
}