예제 #1
0
        /// <summary>
        /// 设定在指定属性发生更改时,通知当前命令的可执行状态已更改。
        /// </summary>
        /// <param name="source">发出属性更改通知的事件源对象。</param>
        /// <param name="propertyName">要接收更改通知的属性的名称。</param>
        /// <returns>当前 <see cref="DelegateCommand"/> 实例。</returns>
        /// <exception cref="ArgumentNullException">
        /// <paramref name="source"/> 为 <see langword="null"/>。</exception>
        public DelegateCommand ObserveCanExecute(
            INotifyPropertyChanged source, string propertyName)
        {
            if (source is null)
            {
                throw new ArgumentNullException(nameof(source));
            }
            var observer = new CanExecuteObserver(this, propertyName);

            source.PropertyChanged += observer.OnPropertyChanged;
            return(this);
        }
예제 #2
0
        public AsyncCommandBuilder ObservesCanExecute([NotNull] Expression <Func <bool> > canExecute, bool fallback)
        {
            if (canExecute == null)
            {
                throw new ArgumentNullException(nameof(canExecute));
            }

            if (this.canExecuteSubject != null)
            {
                throw new CommandBuilderException(ExceptionStrings.CanExecuteExpressionAlreadyDefined);
            }

            if (this.canExecuteFunction != null)
            {
                throw new CommandBuilderException(ExceptionStrings.CanExecuteFunctionAlreadyDefined);
            }

            this.canExecuteSubject = CanExecuteObserver.Create(canExecute, fallback);
            return(this);
        }