日期:2014-05-17 浏览次数:21104 次
public class RelayCommand<T>:ICommand
{
//定义执行命令成员和是否可执行命令成员
readonly Action<T> _execute;
readonly Func<T,bool> _canExecute;
//构造函数
public RelayCommand(Action<T> execute)
: this(execute, null)
{
}
public RelayCommand(Action<T> execute, Func<T,bool> canExecute)
{
if (execute == null)
throw new ArgumentNullException("RelayCommand执行命令不可预知");
_execute = execute;
_canExecute = canExecute;
}
public bool CanExecute(T parameter)
{
return _canExecute == null ? true : _canExecute(parameter);
}
public event EventHandler CanExecuteChanged
{
add { CommandManager.RequerySuggested += value; }
remove { CommandManager.RequerySuggested -= value; }
}
public void Execute(T parameter)
{
_execute(parameter);
}
public bool CanExecute(object parameter)
{
return _canExecute == null ? true : _canExecute((T)parameter);
}
public void Execute(object parameter)
{
_execute((T)parameter);
}
}
public abstract class ViewModelBase&n