protected override DependencyObject GetContainerForItemOverride()
        {
            var visualItem = new AnimatedLayoutControl();

            visualItem.RandomGenerator         = RandomGenerator;
            visualItem.ApplyTemplateCompleted += new EventHandler(visualItem_ApplyTemplateCompleted);
            return(visualItem);
        }
        void ObservableItems_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
        {
            switch (e.Action)
            {
            case NotifyCollectionChangedAction.Add:
                foreach (var item in e.NewItems)
                {
                    if (isUnloading)
                    {
                        NewRequestedItems.Add(item);
                    }
                    else
                    {
                        BaseItemsSource.Add(item);
                    }
                }
                break;

            case NotifyCollectionChangedAction.Reset:
                if (!IsVisible)
                {
                    BaseItemsSource.Clear();
                    VisualItems.Clear();
                    isUnloading = false;
                }
                else if (VisualItems.Count > 0)
                {
                    isUnloading = true;
                    AnimatedLayoutControl lastItem = null;
                    foreach (var item in VisualItems)
                    {
                        if ((lastItem == null) || (item.BeginTime > lastItem.BeginTime))
                        {
                            lastItem = item;
                        }
                        item.LayoutState = LayoutState.Unloaded;
                    }

                    lastItem.LayoutStateChangeCompleted += new EventHandler <LayoutStateChangeEventArgs>(item_LayoutStateChangeCompleted);
                }
                break;
            }
        }
        private void SetItemIndex()
        {
            if (NormalizedIndex != null && !Double.IsNaN(NormalizedIndex.Value))
            {
                return;
            }

            Panel                 parentPanel     = null;
            DependencyObject      parentContainer = null;
            AnimatedLayoutControl target          = this;

            GetParentElements(target, ref parentPanel, ref parentContainer);

            UIElement child = null;

            if (parentContainer != null)
            {
                child = parentContainer as UIElement;
            }
            else
            {
                child = this;
            }

            if (parentPanel != null)
            {
                var itemIndex = parentPanel.Children.IndexOf(child);
                var itemCount = parentPanel.Children.Count - 1;
                if (itemCount == 0)
                {
                    NormalizedIndex = 0;
                }
                else
                {
                    NormalizedIndex = (Double)itemIndex / (Double)itemCount;
                }
            }
        }