private void registerSourceItem(ItemInfo itemInfo, INotifyCollectionChanged source)
        {
            itemInfo.Source = source;

            if (source != null)
            {
                itemInfo.SourceAsINotifyPropertyChanged = (INotifyPropertyChanged)source;

                itemInfo.SourcePropertyChangedEventHandler = (sender, args) =>
                                                             Utils.handleSourcePropertyChanged(args, ref itemInfo.IndexerPropertyChangedEventRaised);

                itemInfo.SourceAsINotifyPropertyChanged.PropertyChanged +=
                    itemInfo.SourcePropertyChangedEventHandler;

                itemInfo.SourceNotifyCollectionChangedEventHandler = (sender, eventArgs) =>
                                                                     handleSourceCollectionChanged(sender, eventArgs, itemInfo);
                source.CollectionChanged += itemInfo.SourceNotifyCollectionChangedEventHandler;

                IHasChangeMarker sourceAsIHasChangeMarker = source as IHasChangeMarker;
                itemInfo.SourceAsIHasChangeMarker = sourceAsIHasChangeMarker;
                if (sourceAsIHasChangeMarker != null)
                {
                    itemInfo.LastProcessedSourceChangeMarker = sourceAsIHasChangeMarker.ChangeMarker;
                }
            }

            initializeSourceCopy(itemInfo);
        }
Пример #2
0
        private void registerSourceItem(ItemInfo itemInfo, INotifyCollectionChanged source)
        {
            itemInfo.Source = source;

            if (source != null)
            {
                itemInfo.SourceAsINotifyPropertyChanged = (INotifyPropertyChanged)source;

                itemInfo.SourcePropertyChangedEventHandler = (sender, args) =>
                {
                    if (args.PropertyName == "Item[]")
                    {
                        itemInfo.IndexerPropertyChangedEventRaised =
                            true;                             // ObservableCollection raises this before CollectionChanged event raising
                    }
                };

                itemInfo.SourceWeakPropertyChangedEventHandler =
                    new WeakPropertyChangedEventHandler(itemInfo.SourcePropertyChangedEventHandler);

                itemInfo.SourceAsINotifyPropertyChanged.PropertyChanged +=
                    itemInfo.SourceWeakPropertyChangedEventHandler.Handle;

                itemInfo.SourceNotifyCollectionChangedEventHandler = (sender, eventArgs) =>
                                                                     handleSourceCollectionChanged(sender, eventArgs, itemInfo);
                itemInfo.SourceWeakNotifyCollectionChangedEventHandler =
                    new WeakNotifyCollectionChangedEventHandler(itemInfo.SourceNotifyCollectionChangedEventHandler);
                if (source != null)
                {
                    source.CollectionChanged += itemInfo.SourceWeakNotifyCollectionChangedEventHandler.Handle;
                }

                IHasChangeMarker sourceAsIHasChangeMarker = source as IHasChangeMarker;
                itemInfo.SourceAsIHasChangeMarker = sourceAsIHasChangeMarker;
                if (sourceAsIHasChangeMarker != null)
                {
                    itemInfo.LastProcessedSourceChangeMarker = sourceAsIHasChangeMarker.ChangeMarker;
                }
            }
        }
Пример #3
0
        private void initializeFromSources()
        {
            int originalCount = _items.Count;

            if (_sourcesNotifyCollectionChangedEventHandler != null)
            {
                int itemInfosCount = _itemInfos.Count;
                for (int index = 0; index < itemInfosCount; index++)
                {
                    ItemInfo itemInfo = _itemInfos[index];
                    if (itemInfo.Source != null)
                    {
                        itemInfo.Source.CollectionChanged -=
                            itemInfo.SourceWeakNotifyCollectionChangedEventHandler.Handle;
                    }
                }

                int capacity = _sourcesScalar != null?Utils.getCapacity(_sourcesScalar) : Utils.getCapacity(_sources);

                _itemInfos            = new List <ItemInfo>(capacity);
                _sourceRangePositions = new RangePositions <ItemInfo>(_itemInfos);

                _sources.CollectionChanged -= _sourcesWeakNotifyCollectionChangedEventHandler.Handle;
                _sourcesNotifyCollectionChangedEventHandler     = null;
                _sourcesWeakNotifyCollectionChangedEventHandler = null;
            }

            if (_sourcesAsINotifyPropertyChanged != null)
            {
                _sourcesAsINotifyPropertyChanged.PropertyChanged -=
                    _sourcesWeakPropertyChangedEventHandler.Handle;

                _sourcesAsINotifyPropertyChanged        = null;
                _sourcesPropertyChangedEventHandler     = null;
                _sourcesWeakPropertyChangedEventHandler = null;
            }

            if (_sourcesScalar != null)
            {
                _sources = _sourcesScalar.Value;
            }
            _sourcesAsList = (IList)_sources;

            if (_sources != null)
            {
                _sourcesAsIHasChangeMarker = _sourcesAsList as IHasChangeMarker;

                if (_sourcesAsIHasChangeMarker != null)
                {
                    _lastProcessedSourcesChangeMarker = _sourcesAsIHasChangeMarker.ChangeMarker;
                }
                else
                {
                    _sourcesAsINotifyPropertyChanged = (INotifyPropertyChanged)_sourcesAsList;

                    _sourcesPropertyChangedEventHandler = (sender, args) =>
                    {
                        if (args.PropertyName == "Item[]")
                        {
                            _indexerPropertyChangedEventRaised = true;                                                        // ObservableCollection raises this before CollectionChanged event raising
                        }
                    };

                    _sourcesWeakPropertyChangedEventHandler =
                        new WeakPropertyChangedEventHandler(_sourcesPropertyChangedEventHandler);

                    _sourcesAsINotifyPropertyChanged.PropertyChanged +=
                        _sourcesWeakPropertyChangedEventHandler.Handle;
                }


                int plainIndex = 0;
                int count      = _sourcesAsList.Count;
                for (int index = 0; index < count; index++)
                {
                    object sourceItemObject = _sourcesAsList[index];
                    IReadScalar <object> sourceItemScalar = sourceItemObject as IReadScalar <object>;
                    IList    sourceItem      = sourceItemScalar != null ? (IList)sourceItemScalar.Value : (IList)sourceItemObject;
                    int      sourceItemCount = sourceItem?.Count ?? 0;
                    ItemInfo itemInfo        = _sourceRangePositions.Add(sourceItemCount);
                    registerSourceItem(sourceItemObject, itemInfo);

                    for (int sourceSourceIndex = 0; sourceSourceIndex < sourceItemCount; sourceSourceIndex++)
                    {
                        if (originalCount > plainIndex)
                        {
                            // ReSharper disable once PossibleNullReferenceException
                            _items[plainIndex++] = (TSourceItem)sourceItem[sourceSourceIndex];
                        }
                        else
                        {
                            // ReSharper disable once PossibleNullReferenceException
                            _items.Insert(plainIndex++, (TSourceItem)sourceItem[sourceSourceIndex]);
                        }
                    }
                }

                for (int index = originalCount - 1; index >= plainIndex; index--)
                {
                    _items.RemoveAt(index);
                }

                _sourcesNotifyCollectionChangedEventHandler     = handleSourcesCollectionChanged;
                _sourcesWeakNotifyCollectionChangedEventHandler =
                    new WeakNotifyCollectionChangedEventHandler(_sourcesNotifyCollectionChangedEventHandler);

                _sources.CollectionChanged += _sourcesWeakNotifyCollectionChangedEventHandler.Handle;
            }
            else
            {
                _items.Clear();
            }

            reset();
        }
Пример #4
0
        private void initializeFromSource()
        {
            int originalCount = _items.Count;

            if (_sourceNotifyCollectionChangedEventHandler != null)
            {
                if (_destinationDispatcher != null)
                {
                    _destinationDispatcher.Invoke(baseClearItems, this);
                }
                else
                {
                    _collectionDestinationDispatcher.Invoke(baseClearItems, this, NotifyCollectionChangedAction.Reset, null, null, 0, 0);
                }

                _source.CollectionChanged -= _sourceWeakNotifyCollectionChangedEventHandler.Handle;
                _sourceNotifyCollectionChangedEventHandler     = null;
                _sourceWeakNotifyCollectionChangedEventHandler = null;
            }

            if (_sourceAsINotifyPropertyChanged != null)
            {
                _sourceAsINotifyPropertyChanged.PropertyChanged -=
                    _sourceWeakPropertyChangedEventHandler.Handle;

                _sourceAsINotifyPropertyChanged        = null;
                _sourcePropertyChangedEventHandler     = null;
                _sourceWeakPropertyChangedEventHandler = null;
            }

            if (_sourceScalar != null)
            {
                _source = _sourceScalar.Value;
            }
            _sourceAsList = _source as IList <TSourceItem>;

            if (_sourceAsList != null)
            {
                _sourceAsIHasChangeMarker = _sourceAsList as IHasChangeMarker;

                if (_sourceAsIHasChangeMarker != null)
                {
                    _lastProcessedSourceChangeMarker = _sourceAsIHasChangeMarker.ChangeMarker;
                }
                else
                {
                    _sourceAsINotifyPropertyChanged = (INotifyPropertyChanged)_sourceAsList;

                    _sourcePropertyChangedEventHandler = (sender1, args1) =>
                    {
                        if (args1.PropertyName == "Item[]")
                        {
                            _indexerPropertyChangedEventRaised =
                                true;                                 // ObservableCollection raises this before CollectionChanged event raising
                        }
                    };

                    _sourceWeakPropertyChangedEventHandler =
                        new WeakPropertyChangedEventHandler(_sourcePropertyChangedEventHandler);

                    _sourceAsINotifyPropertyChanged.PropertyChanged += _sourceWeakPropertyChangedEventHandler.Handle;
                }

                int count = _sourceAsList.Count;
                int sourceIndex;
                for (sourceIndex = 0; sourceIndex < count; sourceIndex++)
                {
                    if (originalCount > sourceIndex)
                    {
                        _items[sourceIndex] = _sourceAsList[sourceIndex];
                    }
                    else
                    {
                        _items.Insert(sourceIndex, _sourceAsList[sourceIndex]);
                    }
                }

                for (int index = originalCount - 1; index >= sourceIndex; index--)
                {
                    _items.RemoveAt(index);
                }

                _sourceNotifyCollectionChangedEventHandler     = handleSourceCollectionChanged;
                _sourceWeakNotifyCollectionChangedEventHandler =
                    new WeakNotifyCollectionChangedEventHandler(_sourceNotifyCollectionChangedEventHandler);
                _source.CollectionChanged += _sourceWeakNotifyCollectionChangedEventHandler.Handle;
            }
            else
            {
                _items.Clear();
            }

            if (_destinationDispatcher != null)
            {
                _destinationDispatcher.Invoke(reset, this);
            }
            else
            {
                _collectionDestinationDispatcher.Invoke(reset, this, NotifyCollectionChangedAction.Reset, null, null, 0, 0);
            }
        }