private void OnSourceCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
        {
            object[] oldInnerCollection = innerCollection;

            innerCollection = TranslateCollection(SourceCollection.Cast <object>(), filterPredicate, sortKeySelector, sortDirection).ToArray();

            if (innerCollection.Contains(CurrentItem))
            {
                SetCurrent(CurrentItem);
            }
            else
            {
                SetCurrent(CurrentItemIndex >= oldInnerCollection.Length ? innerCollection.Length : CurrentItemIndex);
            }

            IEnumerable <object> oldItems = e.OldItems.Intersect(oldInnerCollection).ToArray();
            IEnumerable <object> newItems = e.NewItems.Intersect(innerCollection).ToArray();

            if (oldItems.Count() > 1 || newItems.Count() > 1)
            {
                CollectionChanged.Raise(this, NotifyCollectionChangedEventArgs.Reset(oldInnerCollection, innerCollection));
                return;
            }

            object oldItem  = e.OldItems.FirstOrDefault();
            int    oldIndex = Granular.Compatibility.Array.FindIndex(oldInnerCollection, item => Granular.Compatibility.EqualityComparer <object> .Default.Equals(item, oldItem));

            object newItem  = e.NewItems.FirstOrDefault();
            int    newIndex = Granular.Compatibility.Array.FindIndex(innerCollection, item => Granular.Compatibility.EqualityComparer <object> .Default.Equals(item, newItem));

            if (oldIndex == -1 && newIndex == -1 || oldItem == newItem && oldIndex == newIndex)
            {
                return;
            }

            if (Granular.Compatibility.EqualityComparer <object> .Default.Equals(oldItem, newItem))
            {
                CollectionChanged.Raise(this, NotifyCollectionChangedEventArgs.Move(newItem, oldIndex, newIndex));
            }
            else if (oldIndex == newIndex)
            {
                CollectionChanged.Raise(this, NotifyCollectionChangedEventArgs.Replace(oldItem, newItem, newIndex));
            }
            else
            {
                if (oldIndex != -1)
                {
                    CollectionChanged.Raise(this, NotifyCollectionChangedEventArgs.Remove(oldItem, oldIndex));
                }

                if (newIndex != -1)
                {
                    CollectionChanged.Raise(this, NotifyCollectionChangedEventArgs.Add(newItem, newIndex));
                }
            }
        }
Exemple #2
0
        public bool Remove(UIElement item)
        {
            int index = children.IndexOf(item);

            if (index == -1)
            {
                return(false);
            }

            bool result = children.Remove(item);

            ClearChildParent(item);

            CollectionChanged.Raise(this, NotifyCollectionChangedEventArgs.Remove(item, index));

            return(result);
        }