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,不过不需要理解的,知道需要这个东西就行
WPF MVVM模式开发实现简明教程 2 初识 INotifyPropertyChanged
WPF MVVM模式开发实现简明教程 3-1 BaseCommand
WPF MVVM模式开发实现简明教程 4 ViewModelBase
WPF MVVM模式开发简明实现教程 5 使用MultiValueConverter进行多参数事件绑定