コード例 #1
0
    private void OnSourceCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
    {
        void Add(TKey key, TValue value)
        {
            _view[key] = _selector(value);
            if (value is INotifyPropertyChanged o)
            {
                o.PropertyChanged -= OnSourceItemPropertyChanged;
                o.PropertyChanged += OnSourceItemPropertyChanged;
            }
        }

        void AddRange(IEnumerable enumerable)
        {
            foreach (var(key, value) in enumerable.OfType <KeyValuePair <TKey, TValue> >())
            {
                Add(key, value);
            }
        }

        void Remove(TKey key, TValue value)
        {
            if (_view.ContainsKey(key))
            {
                _view.Remove(key);
            }

            if (value is INotifyPropertyChanged o)
            {
                o.PropertyChanged -= OnSourceItemPropertyChanged;
            }
        }

        void RemoveRange(IEnumerable enumerable)
        {
            foreach (var(key, value) in enumerable.OfType <KeyValuePair <TKey, TValue> >())
            {
                Remove(key, value);
            }
        }

        if (e.Action == NotifyCollectionChangedAction.Add)
        {
            AddRange(e.NewItems);
        }
        else if (e.Action == NotifyCollectionChangedAction.Remove)
        {
            RemoveRange(e.OldItems);
        }
        else if (e.Action == NotifyCollectionChangedAction.Replace)
        {
            RemoveRange(e.OldItems);
            AddRange(e.NewItems);
        }
        else if (e.Action == NotifyCollectionChangedAction.Reset)
        {
            foreach (var(key, value) in _dictionary)
            {
                Remove(key, _dictionary[key]);
            }

            _view.Clear();
        }
        else if (e.Action == NotifyCollectionChangedAction.Move)
        {
        }
    }