Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class DelegateCommandListen : ICommand
- {
- private readonly List<WeakReference> _controlEvent;
- private Action<object> _executeDelegate;
- public DelegateCommandListen(Action<object> executeDelegate, Predicate<object> canExecuteDelegate)
- {
- _controlEvent = new List<WeakReference>();
- ExecuteDelegate = executeDelegate;
- CanExecuteDelegate = canExecuteDelegate;
- }
- public Predicate<object> CanExecuteDelegate { get; set; }
- public Action<object> ExecuteDelegate
- {
- get { return _executeDelegate; }
- set
- {
- _executeDelegate = value;
- ListenForNotificationFrom((INotifyPropertyChanged) _executeDelegate.Target);
- }
- }
- public void RaiseCanExecuteChanged()
- {
- if (_controlEvent != null && _controlEvent.Count > 0)
- _controlEvent.ForEach(ce => { ((EventHandler) ce.Target)?.Invoke(null, EventArgs.Empty); });
- }
- public DelegateCommandListen ListenOn<TObservedType, TPropertyType>
- (TObservedType viewModel, Expression<Func<TObservedType, TPropertyType>> propertyExpression)
- where TObservedType : INotifyPropertyChanged
- {
- var propertyName = GetPropertyName(propertyExpression);
- viewModel.PropertyChanged += (s, e) =>
- {
- if (e.PropertyName == propertyName) RaiseCanExecuteChanged();
- };
- return this;
- }
- public void ListenForNotificationFrom<TObservedType>(TObservedType viewModel)
- where TObservedType : INotifyPropertyChanged
- {
- viewModel.PropertyChanged += (s, e) => RaiseCanExecuteChanged();
- }
- private static string GetPropertyName<T, TProperty>(Expression<Func<T, TProperty>> expression)
- where T : INotifyPropertyChanged
- {
- var lambda = expression as LambdaExpression;
- var memberInfo = GetMemberExpression(lambda).Member;
- return memberInfo.Name;
- }
- private static MemberExpression GetMemberExpression(LambdaExpression lambda)
- {
- MemberExpression memberExpression;
- if (lambda.Body is UnaryExpression body)
- {
- var unaryExpression = body;
- memberExpression = unaryExpression.Operand as MemberExpression;
- }
- else
- memberExpression = lambda.Body as MemberExpression;
- return memberExpression;
- }
- public bool CanExecute(object parameter) => CanExecuteDelegate == null || CanExecuteDelegate(parameter);
- public event EventHandler CanExecuteChanged
- {
- add
- {
- CommandManager.RequerySuggested += value;
- _controlEvent.Add(new WeakReference(value));
- }
- remove
- {
- CommandManager.RequerySuggested -= value;
- _controlEvent.Remove(_controlEvent.Find(r => (EventHandler) r.Target == value));
- }
- }
- public void Execute(object parameter) => ExecuteDelegate?.Invoke(parameter);
- }
- [TestMethod]
- public void NoTarget()
- {
- var sut = new DummyViewModel();
- Assert.IsFalse(sut.IsSelected);
- Assert.IsFalse(sut.ListenWithoutTargetCommand.CanExecute(null));
- sut.IsSelected = true;
- Assert.IsTrue(sut.ListenWithoutTargetCommand.CanExecute(null));
- }
- public class DummyViewModel : INotifyPropertyChanged
- {
- private ICommand _listenWith1TargetCommand;
- private bool _isSelected;
- public string Result { get; set; }
- public bool IsSelected
- {
- get => _isSelected;
- set
- {
- if (value == _isSelected) return;
- _isSelected = value;
- OnPropertyChanged();
- }
- }
- public ICommand ListenWith1TargetCommand
- {
- get
- {
- return _listenWith1TargetCommand ?? (_listenWith1TargetCommand = new DelegateCommandListen(
- s => { Result = "Executing listen command 1"; },
- s => IsSelected)
- .ListenOn(this, o => o.IsSelected));
- }
- }
- public event PropertyChangedEventHandler PropertyChanged;
- [NotifyPropertyChangedInvocator]
- protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
- {
- PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
- }
- }
Add Comment
Please, Sign In to add comment