namespace WpfApp6.ViewModel { using System; using System.Windows.Input; /// <summary> /// 命令基类 /// </summary> public class BaseCommand : ICommand { public event EventHandler CanExecuteChanged { add { if (_canExecute != null) { CommandManager.RequerySuggested += value; } } remove { if (_canExecute != null) { CommandManager.RequerySuggested -= value; } } } public bool CanExecute(object parameter) { if (_canExecute == null) { return true; } return _canExecute(parameter); } public void Execute(object parameter) { if (_execute != null && CanExecute(parameter)) { _execute(parameter); } } private Func<object, bool> _canExecute; private Action<object> _execute; public BaseCommand(Action<object> execute, Func<object, bool> canExecute) { _execute = execute; _canExecute = canExecute; } public BaseCommand(Action<object> execute) : this(execute, null) { } } }
网上的代码,主要是Execute,CanExecute,不过不需要理解的,知道需要这个东西就行