mirror of
https://github.com/holgerb83/ModernWpfPlayground.git
synced 2025-04-16 21:43:51 +02:00
38 lines
943 B
C#
38 lines
943 B
C#
using System;
|
|
using System.Windows.Input;
|
|
|
|
namespace ModernWpfPlayground
|
|
{
|
|
public class RelayCommand : ICommand
|
|
{
|
|
private readonly Predicate<object> _canExecute;
|
|
private readonly Action<object> _execute;
|
|
|
|
public RelayCommand(Action<object> execute)
|
|
{
|
|
_execute = execute;
|
|
}
|
|
|
|
public RelayCommand(Action<object> execute, Predicate<object> canExecute)
|
|
{
|
|
_canExecute = canExecute;
|
|
_execute = execute;
|
|
}
|
|
|
|
public event EventHandler CanExecuteChanged
|
|
{
|
|
add => CommandManager.RequerySuggested += value;
|
|
remove => CommandManager.RequerySuggested -= value;
|
|
}
|
|
|
|
public bool CanExecute(object parameter)
|
|
{
|
|
return _canExecute?.Invoke(parameter) ?? true;
|
|
}
|
|
|
|
public void Execute(object parameter)
|
|
{
|
|
_execute(parameter);
|
|
}
|
|
}
|
|
} |