TRX/tools/installer/TRX_InstallerLib/Utils/RelayCommand.cs
lahm86 e16fcda94b tools/installer: create common installer library
This creates a generic common installer WPF library for both games.
2025-03-30 12:34:54 +01:00

101 lines
2.1 KiB
C#

using System.Windows.Input;
namespace TRX_InstallerLib.Utils;
public class RelayCommand : ICommand
{
public RelayCommand(Action execute, Func<bool>? canExecute)
{
_execute = execute;
_canExecute = canExecute;
}
public RelayCommand(Action execute)
{
_execute = execute;
_canExecute = null;
}
public event EventHandler? CanExecuteChanged
{
add
{
CommandManager.RequerySuggested += value;
_canExecuteChanged += value;
}
remove
{
CommandManager.RequerySuggested -= value;
_canExecuteChanged -= value;
}
}
public bool CanExecute(object? parameter)
{
return _canExecute == null || _canExecute();
}
public void Execute(object? parameter)
{
_execute();
}
public void RaiseCanExecuteChanged()
{
_canExecuteChanged?.Invoke(this, EventArgs.Empty);
}
private readonly Func<bool>? _canExecute;
private readonly Action _execute;
private EventHandler? _canExecuteChanged;
}
public class RelayCommand<T> : ICommand
{
public RelayCommand(Action<T?> execute, Func<T?, bool>? canExecute)
{
_execute = execute;
_canExecute = canExecute;
}
public RelayCommand(Action<T?> execute)
{
_execute = execute;
_canExecute = null;
}
public event EventHandler? CanExecuteChanged
{
add
{
CommandManager.RequerySuggested += value;
_canExecuteChanged += value;
}
remove
{
CommandManager.RequerySuggested -= value;
_canExecuteChanged -= value;
}
}
public bool CanExecute(object? parameter)
{
return _canExecute == null || _canExecute((T?)parameter);
}
public void Execute(object? parameter)
{
_execute((T?)parameter);
}
public void RaiseCanExecuteChanged()
{
_canExecuteChanged?.Invoke(this, EventArgs.Empty);
}
private readonly Func<T?, bool>? _canExecute;
private readonly Action<T?> _execute;
private EventHandler? _canExecuteChanged;
}