首页 > 其他 > 详细

ICommand.CanExecuteChanged事件订阅对象的变化

时间:2015-01-30 01:19:54      阅读:364      评论:0      收藏:0      [点我收藏+]

 

 

 public class DelegateCommand : ICommand
    {
        Func<object, bool> canExecute;
        Action<object> executeAction;
        bool canExecuteCache;
        #region 构造函数
        public DelegateCommand()
            : this(null, null)
        {
        }

        public DelegateCommand(Action<object> executeAction, Func<object, bool> canExecute)
        {
            this.executeAction = executeAction;
            this.canExecute = canExecute;

        }
        #endregion

        #region get-set

        public Func<object, bool> CanExecuteEx
        {
            get { return canExecute; }
            set { canExecute = value; }
        }

        public Action<object> ExecuteActionEx
        {
            get { return executeAction; }
            set { executeAction = value; }
        }

        #endregion

        #region ICommand Members

        public bool CanExecute(object parameter)
        {
            if (canExecute == null) return true;
            bool temp = canExecute(parameter);

            if (canExecuteCache != temp)
            {
                canExecuteCache = temp; 
            }

            return canExecuteCache;
        }

        //public event EventHandler CanExecuteChanged;

        public void Execute(object parameter)
        {
            if (executeAction == null) return;
            executeAction(parameter);
        }
        public event EventHandler CanExecuteChanged
        {
            add { CommandManager.RequerySuggested += value; }
            remove { CommandManager.RequerySuggested -= value; }
        }
        #endregion
    }

  

ICommand.CanExecuteChanged事件订阅对象的变化

原文:http://www.cnblogs.com/qq247039968/p/4261345.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!