94 lines
2.7 KiB
C#
94 lines
2.7 KiB
C#
using System;
|
|
using System.Windows.Input;
|
|
|
|
namespace JD.WPF.Utils
|
|
{
|
|
public class RelayCommand : ICommand
|
|
{
|
|
private readonly Action<object> _execute;
|
|
private readonly Predicate<object> _canExecute;
|
|
|
|
public RelayCommand(Action<object> execute) : this(execute, null) { }
|
|
|
|
public RelayCommand(Action<object> execute, Predicate<object> canExecute)
|
|
{
|
|
if (execute == null)
|
|
throw new ArgumentNullException("execute");
|
|
|
|
_execute = execute;
|
|
_canExecute = canExecute;
|
|
}
|
|
|
|
public bool CanExecute(object parameter)
|
|
{
|
|
return _canExecute == null ? true : _canExecute(parameter);
|
|
}
|
|
|
|
public event EventHandler CanExecuteChanged
|
|
{
|
|
add { CommandManager.RequerySuggested += value; }
|
|
remove { CommandManager.RequerySuggested -= value; }
|
|
}
|
|
|
|
public void Execute(object parameter)
|
|
{
|
|
_execute(parameter);
|
|
}
|
|
}
|
|
|
|
public class RelayCommand<DataType> : ICommand
|
|
{
|
|
private readonly Action<DataType> _execute;
|
|
private readonly Predicate<DataType> _canExecute;
|
|
|
|
public RelayCommand(Action<DataType> execute) : this(execute, null) { }
|
|
|
|
public RelayCommand(Action<DataType> execute, Predicate<DataType> canExecute)
|
|
{
|
|
if (execute == null)
|
|
throw new ArgumentNullException("execute");
|
|
|
|
_execute = execute;
|
|
_canExecute = canExecute;
|
|
}
|
|
|
|
public bool CanExecute(object parameter)
|
|
{
|
|
if (!(parameter is DataType))
|
|
throw new ArgumentException("The command parameter has an invalid type. ");
|
|
return _canExecute == null ? true : _canExecute((DataType)parameter);
|
|
}
|
|
|
|
public event EventHandler CanExecuteChanged
|
|
{
|
|
add { CommandManager.RequerySuggested += value; }
|
|
remove { CommandManager.RequerySuggested -= value; }
|
|
}
|
|
|
|
public void Execute(object parameter)
|
|
{
|
|
if (!(parameter is DataType))
|
|
throw new ArgumentException("The command parameter has an invalid type. ");
|
|
_execute((DataType)parameter);
|
|
}
|
|
}
|
|
|
|
public class PersistentWindowCloseCommand : ICommand
|
|
{
|
|
public bool CanExecute(object parameter)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
public event EventHandler CanExecuteChanged
|
|
{
|
|
add { CommandManager.RequerySuggested += value; }
|
|
remove { CommandManager.RequerySuggested -= value; }
|
|
}
|
|
|
|
public void Execute(object parameter)
|
|
{
|
|
// Do nothing.
|
|
}
|
|
}
|
|
}
|