/// <summary>
        /// Layouts the children. This is where the magic happens
        /// </summary>
        /// <param name="x">The x coordinate.</param>
        /// <param name="y">The y coordinate.</param>
        /// <param name="width">Width.</param>
        /// <param name="height">Height.</param>
        protected override void LayoutChildren(double x, double y, double width, double height)
        {
            LayoutData LayoutData = GetLayoutData(width, height);

            if (LayoutData.VisibleChildCount == 0)
            {
                return;
            }

            double xChild = x;
            double yChild = y;
            int    row    = 0;
            int    column = 0;

            foreach (View child in Children)
            {
                if (!child.IsVisible)
                {
                    continue;
                }

                LayoutChildIntoBoundingRegion(child, new Rectangle(new Point(xChild, yChild), LayoutData.CellSize));

                if (++column == LayoutData.Columns)
                {
                    column = 0;
                    row++;
                    xChild  = x;
                    yChild += RowSpacing + LayoutData.CellSize.Height;
                }
                else
                {
                    xChild += ColumnSpacing + LayoutData.CellSize.Width;
                }
            }
        }
        /// <summary>
        /// Gets the layout data.
        /// </summary>
        /// <returns>The layout data.</returns>
        /// <param name="width">Width.</param>
        /// <param name="height">Height.</param>
        private LayoutData GetLayoutData(double width, double height)
        {
            Size size = new Size(width, height);

            // Check if cached information is available.
            if (_LayoutDataCache.ContainsKey(size))
            {
                return(_LayoutDataCache[size]);
            }

            int        visibleChildCount = 0;
            Size       maxChildSize      = new Size();
            int        rows       = 0;
            int        columns    = 0;
            LayoutData LayoutData = new LayoutData();

            // Enumerate through all the children.

            foreach (View child in Children)
            {
                // Skip invisible children.
                if (!child.IsVisible)
                {
                    continue;
                }
                // Count the visible children.
                visibleChildCount++;

                // Get the child's requested size.
                SizeRequest childSizeRequest = child.Measure(Double.PositiveInfinity, Double.PositiveInfinity);

                // Accumulate the maximum child size.
                maxChildSize.Width  = Math.Max(maxChildSize.Width, childSizeRequest.Request.Width);
                maxChildSize.Height = Math.Max(maxChildSize.Height, childSizeRequest.Request.Height);
            }

            if (visibleChildCount != 0)
            {
                // Calculate the number of rows and columns.
                if (Double.IsPositiveInfinity(width))
                {
                    columns = visibleChildCount;
                    rows    = 1;
                }
                else
                {
                    columns = (int)((width + ColumnSpacing) / (maxChildSize.Width + ColumnSpacing));
                    columns = Math.Max(1, columns);
                    rows    = (visibleChildCount + columns - 1) / columns;
                }

                // Now maximize the cell size based on the layout size.
                Size cellSize = new Size();

                if (Double.IsPositiveInfinity(width))
                {
                    cellSize.Width = maxChildSize.Width;
                }
                else
                {
                    cellSize.Width = (width - ColumnSpacing * (columns - 1)) / columns;
                }

                if (Double.IsPositiveInfinity(height))
                {
                    cellSize.Height = maxChildSize.Height;
                }
                else
                {
                    cellSize.Height = (height - RowSpacing * (rows - 1)) / rows;
                }

                LayoutData = new LayoutData(visibleChildCount, cellSize, rows, columns);
            }

            _LayoutDataCache.Add(size, LayoutData);
            return(LayoutData);
        }