Esempio n. 1
0
        public ValueDisplayViewModel(IEventAggregator eventAggregator, IRegistryFactory registryFactory)
        {
            _registryFactory = registryFactory;

            eventAggregator.Subscribe(this);

            ((ListCollectionView)CollectionViewSource.GetDefaultView(Values)).CustomSort = ValueComparer.Default;

            Values.CollectionChanged += (sender, e) =>
            {
                void SubscribeNewItems()
                {
                    foreach (IValueViewModel item in e.NewItems)
                    {
                        item.Deleted += ValueDeleted;
                    }
                }

                void UnsubscribeOldItems()
                {
                    foreach (IValueViewModel item in e.OldItems)
                    {
                        item.Deleted -= ValueDeleted;
                    }
                }

                switch (e.Action)
                {
                case NotifyCollectionChangedAction.Add:
                    SubscribeNewItems();
                    break;

                case NotifyCollectionChangedAction.Remove:
                    UnsubscribeOldItems();
                    break;

                case NotifyCollectionChangedAction.Replace:
                    UnsubscribeOldItems();
                    SubscribeNewItems();
                    break;

                case NotifyCollectionChangedAction.Reset:
                    Values.Apply(value => value.Deleted += ValueDeleted);
                    break;
                }
            };
        }