20 using System.Collections.Generic;
21 using System.ComponentModel;
22 using System.Linq.Expressions;
23 using System.Windows.Input;
24 using System.Windows.Threading;
47 : this(execute, canExecute: null)
59 _viewModel = viewModel;
72 throw new ArgumentNullException(
"execute");
75 _canExecute = canExecute;
89 : this(execute, canExecute)
91 _viewModel = viewModel;
96 #region ICommand Members 98 public bool CanExecute(
object parameter)
100 return _canExecute == null ?
true : _canExecute(parameter);
103 public event EventHandler CanExecuteChanged
107 CommandManager.RequerySuggested += value;
108 CommandManagerHelper.AddWeakReferenceHandler(ref _canExecuteChangedHandlers, value, 2);
112 CommandManager.RequerySuggested -= value;
113 CommandManagerHelper.RemoveWeakReferenceHandler(_canExecuteChangedHandlers, value);
117 public void Execute(
object parameter)
127 if (!ExceptionHandler.CentralHandler.Manage(
this, e))
134 #endregion // ICommand Members 136 #region Public methods 144 if (_viewModel == null)
146 throw new InvalidOperationException(
147 "To execute 'ListenOn', construct this DelegatingCommand with the parent view model.");
149 _viewModel.PropertyChanged += (
object sender, PropertyChangedEventArgs e) =>
151 if (e.PropertyName == propertyName)
153 if (_viewModel.ViewDispatcher != null)
155 _viewModel.ViewDispatcher.BeginInvoke(
157 () => this.OnCanExecuteChanged()
163 this.OnCanExecuteChanged();
173 #region Protected methods 175 protected virtual void OnCanExecuteChanged()
177 CommandManagerHelper.CallWeakReferenceHandlers(_canExecuteChangedHandlers);
182 #region Private properties 184 readonly Action<object> _execute;
185 readonly Predicate<object> _canExecute;
187 private List<WeakReference> _canExecuteChangedHandlers;
Command that implements ICommand and accepts delegates that contain the command implementation.
DelegatingCommand(Action< object > execute, Predicate< object > canExecute, ViewModelBase viewModel)
Creates a new command object whose executable state is determined by the canExecute method and that ...
DelegatingCommand(Action< object > execute, Predicate< object > canExecute)
Creates a new command object whose executable state is determined by the canExecute method...
DelegatingCommand(Action< object > execute)
Creates a new command object that can always execute.
DelegatingCommand ListenOn(string propertyName)
Makes DelegateCommnand listen on PropertyChanged events of some object, so that DelegateCommnand can ...
DelegatingCommand(Action< object > execute, ViewModelBase viewModel)
Creates a new command object that knows the view model that it belongs to.