35 lines
849 B
C#
35 lines
849 B
C#
using System;
|
|
using System.Windows.Input;
|
|
|
|
namespace ytLive.Helpers;
|
|
|
|
public class RelayCommand<T> : ICommand
|
|
{
|
|
private readonly Action<T?> _execute;
|
|
private readonly Predicate<T?>? _canExecute;
|
|
|
|
public RelayCommand(Action<T?> execute, Predicate<T?>? canExecute = null)
|
|
{
|
|
_execute = execute;
|
|
_canExecute = canExecute;
|
|
}
|
|
|
|
public event EventHandler? CanExecuteChanged
|
|
{
|
|
add => CommandManager.RequerySuggested += value;
|
|
remove => CommandManager.RequerySuggested -= value;
|
|
}
|
|
|
|
public bool CanExecute(object? parameter)
|
|
{
|
|
if (parameter is T t) return _canExecute?.Invoke(t) ?? true;
|
|
return _canExecute?.Invoke(default) ?? true;
|
|
}
|
|
|
|
public void Execute(object? parameter)
|
|
{
|
|
if (parameter is T t) _execute(t);
|
|
else _execute(default);
|
|
}
|
|
}
|