2017-01-09 13:25:52 +03:00
|
|
|
|
using System;
|
|
|
|
|
|
using System.Windows.Input;
|
|
|
|
|
|
|
|
|
|
|
|
namespace BrightSharp.Commands
|
|
|
|
|
|
{
|
|
|
|
|
|
public class RelayCommand : ICommand
|
|
|
|
|
|
{
|
|
|
|
|
|
private readonly Action _methodToExecute;
|
|
|
|
|
|
private readonly Action<object> _methodToExecuteWithParam;
|
|
|
|
|
|
private readonly Func<object, bool> _canExecuteEvaluator;
|
|
|
|
|
|
|
|
|
|
|
|
public event EventHandler CanExecuteChanged
|
|
|
|
|
|
{
|
|
|
|
|
|
add { CommandManager.RequerySuggested += value; }
|
|
|
|
|
|
remove { CommandManager.RequerySuggested -= value; }
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public RelayCommand(Action<object> methodToExecute, Func<object, bool> canExecuteEvaluator = null)
|
|
|
|
|
|
{
|
|
|
|
|
|
_methodToExecuteWithParam = methodToExecute;
|
|
|
|
|
|
_canExecuteEvaluator = canExecuteEvaluator;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2021-04-14 15:28:30 +03:00
|
|
|
|
public RelayCommand(Action methodToExecute)
|
|
|
|
|
|
{
|
|
|
|
|
|
_methodToExecute = methodToExecute;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2017-01-09 13:25:52 +03:00
|
|
|
|
public bool CanExecute(object parameter)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (_canExecuteEvaluator == null)
|
|
|
|
|
|
return true;
|
|
|
|
|
|
else
|
|
|
|
|
|
return _canExecuteEvaluator.Invoke(parameter);
|
|
|
|
|
|
}
|
|
|
|
|
|
public void Execute(object parameter)
|
|
|
|
|
|
{
|
|
|
|
|
|
_methodToExecuteWithParam?.Invoke(parameter);
|
|
|
|
|
|
_methodToExecute?.Invoke();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2021-04-14 15:28:30 +03:00
|
|
|
|
public class RelayCommand<T> : ICommand where T : class
|
2017-01-09 13:25:52 +03:00
|
|
|
|
{
|
|
|
|
|
|
private readonly Action<T> _methodToExecuteWithParam;
|
|
|
|
|
|
private readonly Func<T, bool> _canExecuteEvaluator;
|
|
|
|
|
|
|
|
|
|
|
|
public event EventHandler CanExecuteChanged
|
|
|
|
|
|
{
|
|
|
|
|
|
add { CommandManager.RequerySuggested += value; }
|
|
|
|
|
|
remove { CommandManager.RequerySuggested -= value; }
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public RelayCommand(Action<T> methodToExecute, Func<T, bool> canExecuteEvaluator = null)
|
|
|
|
|
|
{
|
|
|
|
|
|
_methodToExecuteWithParam = methodToExecute;
|
|
|
|
|
|
_canExecuteEvaluator = canExecuteEvaluator;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public bool CanExecute(object parameter)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (_canExecuteEvaluator == null)
|
|
|
|
|
|
return true;
|
2021-04-14 15:28:30 +03:00
|
|
|
|
return _canExecuteEvaluator.Invoke(parameter as T);
|
2017-01-09 13:25:52 +03:00
|
|
|
|
}
|
|
|
|
|
|
public void Execute(object parameter)
|
|
|
|
|
|
{
|
2021-04-14 15:28:30 +03:00
|
|
|
|
_methodToExecuteWithParam?.Invoke(parameter as T);
|
2017-01-09 13:25:52 +03:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|