using System;
using System.Windows.Input;
namespace Artemis.UI.Shared
{
///
/// Provides a command that simply calls a delegate when invoked
///
public class DelegateCommand : ICommand
{
private readonly Predicate? _canExecute;
private readonly Action _execute;
///
/// Creates a new instance of the class
///
/// The delegate to execute
public DelegateCommand(Action execute) : this(execute, null)
{
}
///
/// Creates a new instance of the class with a predicate indicating whether the command
/// can be executed
///
/// The delegate to execute
/// The predicate that determines whether the command can execute
public DelegateCommand(Action execute, Predicate? canExecute)
{
_execute = execute;
_canExecute = canExecute;
}
///
/// Invokes the event
///
public void RaiseCanExecuteChanged()
{
CanExecuteChanged?.Invoke(this, EventArgs.Empty);
}
///
public event EventHandler? CanExecuteChanged;
///
public bool CanExecute(object? parameter)
{
if (_canExecute == null)
return true;
return _canExecute(parameter);
}
///
public void Execute(object? parameter)
{
_execute(parameter);
}
}
}