12345678910111213141516171819202122232425262728293031323334353637383940414243444546 |
- using System;
- using System.Windows.Input;
- namespace UpdateDataTool
- {
- public class RelayCommand : ICommand
- {
- private readonly Action<object> _executeAction;
- private readonly Func<object, bool> _canExecute;
- /// <summary>
- /// Occurs when changes occur that affect whether or not the command should execute.
- /// </summary>
- public event EventHandler CanExecuteChanged;
- /// <summary>
- /// Initializes a new instance of the <see cref="RelayCommand"/> class.
- /// </summary>
- /// <param name="execute">
- /// The action that will be executed.
- /// </param>
- /// <param name="canExecute">The function that indicates whether the command can be executed.</param>
- public RelayCommand(Action execute, Func<bool> canExecute = null)
- : this(p => execute(), canExecute == null ? (Func<object, bool>)null : p => canExecute())
- {
- }
- public RelayCommand(Action<object> executeAction, Func<object, bool> canExecute = null)
- {
- _executeAction = executeAction;
- _canExecute = canExecute;
- }
- public void Execute(object parameter)
- {
- if (CanExecute(parameter))
- {
- _executeAction(parameter);
- }
- }
- public bool CanExecute(object parameter)
- {
- return _canExecute == null || _canExecute(parameter);
- }
- }
- }
|