/// <summary>
        /// Ensures the child item has the appropriate TranslateTransform to perform the animation.
        /// </summary>
        /// <param name="child">the child item.</param>
        /// <returns>Transform Information.</returns>
        private TransformInformation ConfirmTransform(FrameworkElement child)
        {
            TransformInformation transformInformation = new TransformInformation();

            // Check if TransformGroup already exists
            if (child.RenderTransform.GetType() != typeof(TransformGroup))
            {
                // Create TranslateTransform and TransformGroup
                TransformGroup     transformGroup = new TransformGroup();
                TranslateTransform translate      = new TranslateTransform();
                transformGroup.Children.Add(translate);
                child.RenderTransform       = transformGroup;
                child.RenderTransformOrigin = new Point(0.5, 0.5);

                // Initialise the TransformInformation class
                transformInformation = new TransformInformation(translate, 0);
            }
            else
            {
                // Check that a RenderTransform object exists within the TransformGroup
                TransformGroup     transformGroup = (TransformGroup)child.RenderTransform;
                TranslateTransform translate      = new TranslateTransform();

                bool needsGenerating      = true;
                int  positionInCollection = 0;

                for (int i = 0; i < transformGroup.Children.Count; i++)
                {
                    if (transformGroup.Children[i].GetType() == typeof(TranslateTransform))
                    {
                        translate            = (TranslateTransform)transformGroup.Children[i];
                        positionInCollection = i;
                        needsGenerating      = false;
                        break;
                    }
                }

                if (needsGenerating)
                {
                    // Add new TranslateTransform and add to current TransformGroup
                    transformGroup.Children.Add(translate);
                    positionInCollection = transformGroup.Children.Count - 1;
                }

                // Set Transform Properties
                child.RenderTransform       = transformGroup;
                child.RenderTransformOrigin = new Point(0.5, 0.5);

                // Create TransformInformation
                transformInformation = new TransformInformation(translate, positionInCollection);
            }

            return(transformInformation);
        }
        /// <summary>
        /// Ensures the child item has the appropriate TranslateTransform to perform the animation.
        /// </summary>
        /// <param name="child">the child item.</param>
        /// <returns>Transform Information.</returns>
        private TransformInformation ConfirmTransform(FrameworkElement child)
        {
            TransformInformation transformInformation = new TransformInformation();

            // Check if TransformGroup already exists
            if (child.RenderTransform.GetType() != typeof(TransformGroup))
            {
                // Create TranslateTransform and TransformGroup
                TransformGroup transformGroup = new TransformGroup();
                TranslateTransform translate = new TranslateTransform();
                transformGroup.Children.Add(translate);
                child.RenderTransform = transformGroup;
                child.RenderTransformOrigin = new Point(0.5, 0.5);

                // Initialise the TransformInformation class
                transformInformation = new TransformInformation(translate, 0);
            }
            else
            {
                // Check that a RenderTransform object exists within the TransformGroup
                TransformGroup transformGroup = (TransformGroup)child.RenderTransform;
                TranslateTransform translate = new TranslateTransform();

                bool needsGenerating = true;
                int positionInCollection = 0;

                for (int i = 0; i < transformGroup.Children.Count; i++)
                {
                    if (transformGroup.Children[i].GetType() == typeof(TranslateTransform))
                    {
                        translate = (TranslateTransform)transformGroup.Children[i];
                        positionInCollection = i;
                        needsGenerating = false;
                        break;
                    }
                }

                if (needsGenerating)
                {
                    // Add new TranslateTransform and add to current TransformGroup
                    transformGroup.Children.Add(translate);
                    positionInCollection = transformGroup.Children.Count - 1;
                }

                // Set Transform Properties
                child.RenderTransform = transformGroup;
                child.RenderTransformOrigin = new Point(0.5, 0.5);

                // Create TransformInformation
                transformInformation = new TransformInformation(translate, positionInCollection);
            }

            return transformInformation;
        }
        /// <summary>
        /// Performs the layout for all child controls.
        /// </summary>
        /// <param name="finalSize">The space available for the panel to display its child objects.</param>
        /// <returns>Size of the panel.</returns>
        protected override Size ArrangeOverride(Size finalSize)
        {
            base.ArrangeOverride(finalSize);

            double top            = 0;
            double left           = 0;
            bool   widthInfinity  = false;
            bool   heightInfinity = false;
            double greatestHeight = 0;
            double greatestWidth  = 0;

            // Check finalSize does not return infinity
            if (double.IsInfinity(finalSize.Width))
            {
                widthInfinity = true;
            }

            if (double.IsInfinity(finalSize.Height))
            {
                heightInfinity = true;
            }

            foreach (FrameworkElement child in this.Children)
            {
                if (this.wrapDirection == WrapDirection.Horizontal)
                {
                    // If infinite space is available, keep positioning left to right
                    // If not, check to see if we have reached our furthest position
                    if (!widthInfinity)
                    {
                        if (left + child.DesiredSize.Width > finalSize.Width)
                        {
                            // Set top for new line and reset left and greatestHeight properties
                            top += greatestHeight;

                            left           = 0;
                            greatestHeight = 0;
                        }
                    }
                }
                else
                {
                    // If infinite space is available, keep positioning top to bottom
                    // If not, check to see if we have reached our furthest position
                    if (!heightInfinity)
                    {
                        if (top + child.DesiredSize.Height > finalSize.Height)
                        {
                            // Set top for new line and reset left and greatestHeight properties
                            left += greatestWidth;

                            top           = 0;
                            greatestWidth = 0;
                        }
                    }
                }

                // Create child within panel
                child.Arrange(new Rect(0, 0, child.DesiredSize.Width, child.DesiredSize.Height));

                // Ensure the child item has a unique name
                this.ConfirmItemName(child);

                // Get the information about the Transform applied to the item
                TransformInformation information = this.ConfirmTransform(child);

                TranslateTransform translate = information.TranslateTransform;

                // Set child element position to where it is currently
                if (!this.AnimateOnInitialise && !this.performedInitialLayout)
                {
                    // Set in correct position
                    translate.X = left;
                    translate.Y = top;
                }

                // Get the Storyboard for the child item
                Storyboard storyboard = this.ConfirmStoryboard(child, information.PositionInCollection);

                // Configure animation properties for translateX
                DoubleAnimation leftAnim = (DoubleAnimation)storyboard.Children[0];
                leftAnim.To = left;

                // Configure animation properties for translateX
                DoubleAnimation topAnim = (DoubleAnimation)storyboard.Children[1];
                topAnim.To = top;

                // Check if the item is new and set FROM property accordingly
                if (!GetDisableEntrance(child) && this.EntranceAnimationEnabled)
                {
                    // Set FROM Property
                    Point enterFrom = this.GetEntryPosition(child);
                    leftAnim.From = enterFrom.X;
                    topAnim.From  = enterFrom.Y;
                    translate.X   = enterFrom.X;
                    translate.Y   = enterFrom.Y;
                    SetDisableEntrance(child, true);
                }
                else if (!GetDisableEntrance(child) && !this.EntranceAnimationEnabled)
                {
                    leftAnim.From = left;
                    topAnim.From  = top;
                    SetDisableEntrance(child, true);
                }
                else
                {
                    leftAnim.From = null;
                    topAnim.From  = null;
                    SetDisableEntrance(child, true);
                }

                // Begin the Storyboard
                storyboard.Begin();

#if SILVERLIGHT
                // Check if the item is a design-time entrance preview
                // Provide design-time preview of entrance
                if (DesignerProperties.GetIsInDesignMode(Application.Current.RootVisual))
                {
                    storyboard.Completed += new EventHandler(this.EntrancePreviewStoryboard_Completed);
                }
#endif

                // Increment the positioning variables based on orientation
                if (this.wrapDirection == WrapDirection.Horizontal)
                {
                    left += child.DesiredSize.Width;
                }
                else
                {
                    top += child.DesiredSize.Height;
                }

                // Keep track of greatest height and width on this line of items
                if (child.DesiredSize.Width > greatestWidth)
                {
                    greatestWidth = child.DesiredSize.Width;
                }

                if (child.DesiredSize.Height > greatestHeight)
                {
                    greatestHeight = child.DesiredSize.Height;
                }
            }

            // Indicate that the panel has completed the first layout pass
            this.performedInitialLayout = true;

            // Clip out of bound content
            RectangleGeometry rectangleGeometry = new RectangleGeometry();
            Rect clip = new Rect(new Point(0, 0), new Size(finalSize.Width, finalSize.Height));
            rectangleGeometry.Rect = clip;
            this.Clip = rectangleGeometry;

            return(finalSize);
        }