Пример #1
0
 private bool GetIsFadeAnimationEnabled(ItemsControl itemsControl)
 {
     if (itemsControl.ItemsSource is ICollectionView)
     {
         return(((ICollectionView)itemsControl.ItemsSource).SourceCollection == this &&
                CrossThreadCollectionWrapper.GetEnableItemFadeAnimations(itemsControl));
     }
     else
     {
         return(itemsControl.ItemsSource == this &&
                CrossThreadCollectionWrapper.GetEnableItemFadeAnimations(itemsControl));
     }
 }
Пример #2
0
        private void NotifyCollectionChanged(NotifyCollectionChangedEventArgs e)
        {
            if (e.NewItems != null && e.NewItems.Count > 1 ||
                e.OldItems != null && e.OldItems.Count > 1)
            {
                throw new InvalidOperationException("Collection Wrapper extension currently supports no batch add/remove/move");
            }
            switch (e.Action)
            {
            case NotifyCollectionChangedAction.Add:
                if (e.NewStartingIndex == this.internalCollection.Count)
                {
                    this.internalCollection.Add(e.NewItems[0]);
                }
                else
                {
                    this.internalCollection.Insert(e.NewStartingIndex, e.NewItems[0]);
                }
                foreach (ItemsControl ItemsControl in CollectionWrapper.RegisteredControls.Where(this.GetIsFadeAnimationEnabled))
                {
                    UIElement Element = ItemsControl.ItemContainerGenerator.ContainerFromItem(e.NewItems[0]) as UIElement;
                    if (Element != null)
                    {
                        Storyboard Storyboard = CrossThreadCollectionWrapper.GetFadeInAnimation(Element);
                        if (Storyboard != null)
                        {
                            Storyboard MyStoryboard = Storyboard.Clone();
                            MyStoryboard.Freeze();
                            MyStoryboard.Begin((FrameworkElement)Element);
                        }
                    }
                }
                break;

            case NotifyCollectionChangedAction.Remove:
                Dictionary <UIElement, Storyboard> Animations = new Dictionary <UIElement, Storyboard>();
                object Item = e.OldItems[0];
                foreach (ItemsControl ItemsControl in CollectionWrapper.RegisteredControls.Where(this.GetIsFadeAnimationEnabled))
                {
                    UIElement Element = ItemsControl.ItemContainerGenerator.ContainerFromItem(Item) as UIElement;
                    if (Element != null)
                    {
                        Storyboard Storyboard = CrossThreadCollectionWrapper.GetFadeOutAnimation(Element);
                        if (Storyboard != null)
                        {
                            Storyboard MyStoryboard = Storyboard.Clone();
                            MyStoryboard.Completed += delegate
                            {
                                Animations.Remove(Element);
                                if (Animations.Count == 0)
                                {
                                    this.internalCollection.Remove(Item);
                                }
                            };
                            MyStoryboard.Freeze();
                            Animations.Add(Element, MyStoryboard);
                        }
                    }
                }
                if (Animations.Count > 0)
                {
                    Animations.ForEach(_ => _.Value.Begin((FrameworkElement)_.Key));
                }
                else
                {
                    this.internalCollection.Remove(e.OldItems[0]);
                }
                break;

            case NotifyCollectionChangedAction.Replace:
                throw new InvalidOperationException("Collection Wrapper extension currently supports no replace");

            case NotifyCollectionChangedAction.Move:
                this.internalCollection.Move(e.OldStartingIndex, e.NewStartingIndex);
                break;

            case NotifyCollectionChangedAction.Reset:
                this.internalCollection.Clear();
                this.originalCollection.ForEach(item => this.internalCollection.Add(item));
                break;

            default:
                throw new ArgumentOutOfRangeException();
            }
        }