WPF MVVM模式开发实现简明教程 3-1 BaseCommand

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模式开发实现简明教程 3-1 BaseCommand

上一篇:WPF MVVM模式开发简明实现教程 3 事件绑定


下一篇:计算Winform程序的运行时间