mirror of
https://github.com/WowUp/WowUp.git
synced 2026-04-23 15:27:03 -04:00
66 lines
1.9 KiB
C#
66 lines
1.9 KiB
C#
using System;
|
|
using System.Windows.Input;
|
|
|
|
namespace WowUp.WPF.Utilities
|
|
{
|
|
public class Command : ICommand
|
|
{
|
|
readonly Func<object, bool> _canExecute;
|
|
readonly Action<object> _execute;
|
|
readonly WeakEventManager _weakEventManager = new WeakEventManager();
|
|
public Command(Action<object> execute)
|
|
{
|
|
if (execute == null)
|
|
throw new ArgumentNullException(nameof(execute));
|
|
|
|
_execute = execute;
|
|
}
|
|
|
|
public Command(Action execute) : this(o => execute())
|
|
{
|
|
if (execute == null)
|
|
throw new ArgumentNullException(nameof(execute));
|
|
}
|
|
|
|
public Command(Action<object> execute, Func<object, bool> canExecute) : this(execute)
|
|
{
|
|
if (canExecute == null)
|
|
throw new ArgumentNullException(nameof(canExecute));
|
|
|
|
_canExecute = canExecute;
|
|
}
|
|
|
|
public Command(Action execute, Func<bool> canExecute) : this(o => execute(), o => canExecute())
|
|
{
|
|
if (execute == null)
|
|
throw new ArgumentNullException(nameof(execute));
|
|
if (canExecute == null)
|
|
throw new ArgumentNullException(nameof(canExecute));
|
|
}
|
|
|
|
public bool CanExecute(object parameter)
|
|
{
|
|
if (_canExecute != null)
|
|
return _canExecute(parameter);
|
|
|
|
return true;
|
|
}
|
|
|
|
public event EventHandler CanExecuteChanged
|
|
{
|
|
add { _weakEventManager.AddEventHandler(value); }
|
|
remove { _weakEventManager.RemoveEventHandler(value); }
|
|
}
|
|
|
|
public void Execute(object parameter)
|
|
{
|
|
_execute(parameter);
|
|
}
|
|
|
|
public void ChangeCanExecute()
|
|
{
|
|
_weakEventManager.HandleEvent(this, EventArgs.Empty, nameof(CanExecuteChanged));
|
|
}
|
|
}
|
|
}
|