using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;
namespace Eventmaker.Common
{
///
/// A command whose sole purpose is to relay its functionality
/// to other objects by invoking delegates.
/// The default return value for the CanExecute method is 'true'.
/// needs to be called whenever
/// is expected to return a different value.
///
public class RelayCommand : ICommand
{
private readonly Action _execute;
private readonly Func _canExecute;
///
/// Raised when RaiseCanExecuteChanged is called.
///
public event EventHandler CanExecuteChanged;
///
/// Creates a new command that can always execute.
///
/// The execution logic.
public RelayCommand(Action execute)
: this(execute, null)
{
}
///
/// Creates a new command.
///
/// The execution logic.
/// The execution status logic.
public RelayCommand(Action execute, Func canExecute)
{
if (execute == null)
throw new ArgumentNullException("execute");
_execute = execute;
_canExecute = canExecute;
}
///
/// Determines whether this can execute in its current state.
///
///
/// Data used by the command. If the command does not require data to be passed, this object can be set to null.
///
/// true if this command can be executed; otherwise, false.
public bool CanExecute(object parameter)
{
return _canExecute == null ? true : _canExecute();
}
///
/// Executes the on the current command target.
///
///
/// Data used by the command. If the command does not require data to be passed, this object can be set to null.
///
public void Execute(object parameter)
{
_execute();
}
///
/// Method used to raise the event
/// to indicate that the return value of the
/// method has changed.
///
public void RaiseCanExecuteChanged()
{
var handler = CanExecuteChanged;
if (handler != null)
{
handler(this, EventArgs.Empty);
}
}
}
}