Microscopic Traffic Simulator
RelayCommand.cs
Go to the documentation of this file.
1 using System;
2 using System.Diagnostics;
3 using System.Windows.Input;
4 
6 {
10  class RelayCommand : ICommand
11  {
15  readonly Action<object> _execute;
16 
20  readonly Predicate<object> _canExecute;
21 
26  public RelayCommand(Action<object> execute) : this(execute, null) { }
27 
33  public RelayCommand(Action<object> execute, Predicate<object> canExecute)
34  {
35  if (execute == null)
36  throw new ArgumentNullException("execute");
37 
38  _execute = execute;
39  _canExecute = canExecute;
40  }
41 
47  [DebuggerStepThrough]
48  public bool CanExecute(object parameter)
49  {
50  return _canExecute == null ? true : _canExecute(parameter);
51  }
52 
56  public event EventHandler CanExecuteChanged
57  {
58  add { CommandManager.RequerySuggested += value; }
59  remove { CommandManager.RequerySuggested -= value; }
60  }
61 
66  public virtual void Execute(object parameter)
67  {
68  _execute(parameter);
69  }
70  }
71 }
RelayCommand(Action< object > execute)
Creates a new command that can always execute.
Definition: RelayCommand.cs:26
RelayCommand(Action< object > execute, Predicate< object > canExecute)
Creates a new command.
Definition: RelayCommand.cs:33
Command class for binding UI commands with view models.
Definition: RelayCommand.cs:10
EventHandler CanExecuteChanged
For connecting with Command Manager
Definition: RelayCommand.cs:57
virtual void Execute(object parameter)
Execute action
Definition: RelayCommand.cs:66
bool CanExecute(object parameter)
Method for getting if command is possible to run.
Definition: RelayCommand.cs:48