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

 

 

 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
    }

  

posted @ 2015-01-30 00:12  玖彩技术团队  阅读(904)  评论(0编辑  收藏  举报
以上只是个人想法和实践经验,如果有文字错误和语法错误,请加以指点! QQ:247039968 emil:wujc@younger.com 无论是美女的歌声,还是鬣狗的狂吠,无论是鳄鱼的眼泪,还是恶狼的嚎叫,都不会使我动摇