The representation of how to use MVVM architecture the right way!
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

45 lines
1.6 KiB

using System;
using System.Windows.Input;
namespace SignalsMVVM.HelperClasses
{
public class RelayCommand<T> : ICommand
{
protected Action<T> _execute;
protected Func<T, bool> _canExecute;
protected RelayCommand() { }
public RelayCommand(Action<T> execute, Func<T, bool> canExecute)
{
if (execute == null)
throw new ArgumentNullException(nameof(execute));
_execute = execute;
_canExecute = canExecute ?? (p => true);
}
public RelayCommand(Action<T> execute) : this(execute, (Func<T, bool>)null) { }
public RelayCommand(Action<T> execute, Func<bool> canExecute) : this(execute, p => canExecute()) { }
public event EventHandler CanExecuteChanged
{
add { CommandManager.RequerySuggested += value; }
remove { CommandManager.RequerySuggested -= value; }
}
public void Execute(object parameter) => _execute((T)parameter);
public bool CanExecute(object parameter) => _canExecute((T)parameter);
}
public class RelayCommand : RelayCommand<object>
{
public RelayCommand(Action<object> execute, Func<object, bool> canExecute) : base(execute, canExecute) { }
public RelayCommand(Action execute) : base(p => execute()) { }
public RelayCommand(Action execute, Func<bool> canExecute) : base(p => execute(), p => canExecute()) { }
public RelayCommand(Action<object> execute) : base(execute) { }
public RelayCommand(Action<object> execute, Func<bool> canExecute) : base(execute, canExecute) { }
}
}