1
0
mirror of synced 2024-11-15 01:33:51 +00:00
PettingZoo/Infrastructure/DelegateCommand.cs

82 lines
1.8 KiB
C#
Raw Normal View History

2016-06-18 14:50:32 +00:00
using System;
using System.Windows.Input;
namespace PettingZoo.Infrastructure
{
public class DelegateCommand<T> : ICommand
{
private readonly Func<T, bool> canExecute;
private readonly Action<T> execute;
public event EventHandler CanExecuteChanged;
public DelegateCommand(Action<T> execute) : this(execute, null)
{
}
public DelegateCommand(Action<T> execute, Func<T, bool> canExecute)
{
this.execute = execute;
this.canExecute = canExecute;
}
public bool CanExecute(object parameter)
{
return canExecute == null || canExecute((T)parameter);
}
public void Execute(object parameter)
{
execute((T)parameter);
}
public void RaiseCanExecuteChanged()
{
if (CanExecuteChanged != null)
CanExecuteChanged(this, EventArgs.Empty);
}
}
public class DelegateCommand : ICommand
{
private readonly Func<bool> canExecute;
private readonly Action execute;
public event EventHandler CanExecuteChanged;
public DelegateCommand(Action execute) : this(execute, null) { }
public DelegateCommand(Action execute, Func<bool> canExecute)
{
this.execute = execute;
this.canExecute = canExecute;
}
public bool CanExecute(object parameter)
{
return canExecute == null || canExecute();
}
public void Execute(object parameter)
{
execute();
}
public void RaiseCanExecuteChanged()
{
if (CanExecuteChanged != null)
CanExecuteChanged(this, EventArgs.Empty);
}
}
}