protected override Size MeasureOverride(Size availableSize)
        {
            bool isFirst = true;

            foreach (var child in Children)
            {
                PanelContentDirection direction = GetDirection(child);
                int         size        = GetSize(child);
                int         position    = GetPosition(child);
                Orientation orientation = GetOrientation(child);
                bool        snapToLines = GetSnapToLines(child);
                //int? linePosition = GetLinePosition(child);
                PanelContentAlignment alignment = GetAlignment(child);

                bool isHorizontal = orientation == Orientation.Horizontal;
                bool ltr          = direction == PanelContentDirection.LeftToRight;

                if (isFirst)
                {
                    child.Measure(new Size(double.MaxValue, double.MaxValue));
                    lineSize = isHorizontal ? child.DesiredSize.Height : child.DesiredSize.Width;
                    isFirst  = false;
                }

                int maxSize    = GetMaxSize(alignment, position, ltr);
                int actualSize = Math.Min(size, maxSize);

                int directionalPosition = GetPosition(ltr, isHorizontal, alignment, actualSize, position);
                //int orthagonalPosition = snapToLines ? 0 : linePosition.GetValueOrDefault(0);

                if (snapToLines)
                {
                    int maxPosition = isHorizontal ? (int)PaddingPercent.Left : (int)PaddingPercent.Top;
                    directionalPosition = Math.Max(directionalPosition, maxPosition);
                    int maxActualSize = 100 - (isHorizontal ? (int)PaddingPercent.Right : (int)PaddingPercent.Bottom) - directionalPosition;
                    actualSize = Math.Min(actualSize, maxActualSize);
                }
                //int x = isHorizontal ? directionalPosition : orthagonalPosition;
                //int y = !isHorizontal ? directionalPosition : orthagonalPosition;

                if (isHorizontal)
                {
                    double width = availableSize.Width * actualSize / 100;
                    child.Measure(new Size(width, double.MaxValue));
                }
                else
                {
                    double height = availableSize.Height * actualSize / 100;
                    child.Measure(new Size(double.MaxValue, height));
                }
            }
            return(base.MeasureOverride(availableSize));
        }
        protected override Size ArrangeOverride(Size finalSize)
        {
            double topEdge    = finalSize.Height * PaddingPercent.Top / 100;
            double bottomEdge = finalSize.Height - finalSize.Height * PaddingPercent.Bottom / 100;
            double leftEdge   = finalSize.Width * PaddingPercent.Left / 100;
            double rightEdge  = finalSize.Width - finalSize.Width * PaddingPercent.Right / 100;

            double stackingHeight   = bottomEdge;
            double stackingRtlWidth = rightEdge;
            double stackingLtrWidth = leftEdge;

            foreach (var child in Children)
            {
                PanelContentDirection direction    = GetDirection(child);
                int                   size         = GetSize(child);
                int                   position     = GetPosition(child);
                Orientation           orientation  = GetOrientation(child);
                bool                  snapToLines  = GetSnapToLines(child);
                int?                  linePosition = GetLinePosition(child);
                PanelContentAlignment alignment    = GetAlignment(child);

                bool isHorizontal = orientation == Orientation.Horizontal;
                bool ltr          = direction == PanelContentDirection.LeftToRight;

                int maxSize    = GetMaxSize(alignment, position, ltr);
                int actualSize = Math.Min(size, maxSize);

                int directionalPosition = GetPosition(ltr, isHorizontal, alignment, actualSize, position);
                int orthagonalPosition  = snapToLines ? 0 : linePosition.GetValueOrDefault(0);

                if (snapToLines)
                {
                    int maxPosition = isHorizontal ? (int)PaddingPercent.Left : (int)PaddingPercent.Top;
                    directionalPosition = Math.Max(directionalPosition, maxPosition);
                    int maxActualSize = 100 - (isHorizontal ? (int)PaddingPercent.Right : (int)PaddingPercent.Bottom) - directionalPosition;
                    actualSize = Math.Min(actualSize, maxActualSize);
                }

                int x = isHorizontal ? directionalPosition : orthagonalPosition;
                int y = !isHorizontal ? directionalPosition : orthagonalPosition;

                double left = finalSize.Width * x / 100;
                double top = finalSize.Height * y / 100;
                double width, height;

                if (isHorizontal)
                {
                    width  = finalSize.Width * actualSize / 100;
                    height = Math.Min(finalSize.Height, child.DesiredSize.Height);
                }
                else
                {
                    height = finalSize.Height * actualSize / 100;
                    width  = Math.Min(finalSize.Width, child.DesiredSize.Width);
                }

                if (snapToLines)
                {
                    if (!linePosition.HasValue)
                    {
                        if (isHorizontal)
                        {
                            top            = stackingHeight - height;
                            stackingHeight = top;
                        }
                        else if (ltr)
                        {
                            left             = stackingLtrWidth;
                            stackingLtrWidth = left + width;
                        }
                        else
                        {
                            left             = stackingRtlWidth - width;
                            stackingRtlWidth = left;
                        }
                    }
                    else if (linePosition.Value >= 0)
                    {
                        if (isHorizontal)
                        {
                            top = lineSize * linePosition.Value + topEdge;
                        }
                        else if (ltr)
                        {
                            left = lineSize * linePosition.Value + leftEdge;
                        }
                        else
                        {
                            left = rightEdge - lineSize * (linePosition.Value + 1);
                        }
                    }
                    else // negative linePosition
                    {
                        if (isHorizontal)
                        {
                            top = bottomEdge + lineSize * linePosition.Value;
                        }
                        else if (ltr)
                        {
                            left = rightEdge - lineSize * (-linePosition.Value + 1);
                        }
                        else
                        {
                            left = lineSize * -linePosition.Value + leftEdge;
                        }
                    }
                }
                child.Arrange(new Rect(left, top, width, height));
            }
            return(base.ArrangeOverride(finalSize));
        }
 public static void SetDirection(DependencyObject obj, PanelContentDirection value)
 {
     obj.SetValue(DirectionProperty, value);
 }
 public static void SetDirection(DependencyObject obj, PanelContentDirection value)
 {
     obj.SetValue(DirectionProperty, value);
 }