Exemplo n.º 1
0
        public void DoYourThing()
        {
            var collection = new ObservableCollection <ObserverThing>();

            SelectedCollection = new ObservableCollection <ObserverThing>();

            collection.CollectionChanged         += Collection_CollectionChanged;
            SelectedCollection.CollectionChanged += SelectedCollection_CollectionChanged;

            {
                var first  = new ObserverThing("First");
                var second = new ObserverThing("Second");
                var third  = new ObserverThing("Third");
                collection.Add(first);
                collection.Add(second);
                collection.Add(third);

                first.IsSelected  = true;
                second.IsSelected = true;
                third.IsSelected  = true;


                first.IsSelected = false;
                //second.IsSelected = false;
                third.IsSelected = false;

                Console.WriteLine("Removing 'second' from the global list ...");
                collection.Remove(second);
            }
        }
Exemplo n.º 2
0
        private void Thing_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            if (e.PropertyName == nameof(ObserverThing.IsSelected))
            {
                ObserverThing thing = (ObserverThing)sender;

                if (thing.IsSelected == true)
                {
                    SelectedCollection.Add(thing);
                }
                else
                {
                    SelectedCollection.Remove(thing);
                }
            }
        }
Exemplo n.º 3
0
        private void Collection_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
        {
            switch (e.Action)
            {
            case NotifyCollectionChangedAction.Add:
                ObserverThing newThing = (ObserverThing)e.NewItems[0];
                newThing.PropertyChanged += Thing_PropertyChanged;
                break;

            case NotifyCollectionChangedAction.Remove:
                var oldThing = (ObserverThing)e.OldItems[0];
                oldThing.IsSelected       = false;
                oldThing.PropertyChanged -= Thing_PropertyChanged;
                break;

            case NotifyCollectionChangedAction.Move:
            case NotifyCollectionChangedAction.Replace:
            case NotifyCollectionChangedAction.Reset:
                throw new NotImplementedException($"Collection_CollectionChanged, Action:{e.Action}");
            }
        }