protected override Size MeasureOverride(Size availableSize) { // Remove the border and padding, measure the element, and then add them back, as they must be ignored. availableSize = availableSize.Remove(BorderThickness).Remove(Padding); var size = base.MeasureOverride(availableSize) + BorderThickness + Padding; return size; }
/// <summary> /// Measures the element. /// </summary> protected virtual Size MeasureCore(Size availableSize) { // Remove the margin from the size, as it should be ignored for now. availableSize = availableSize.Remove(Margin); // If the width or height is NaN or 0, fall back to the available size, and clamp it within the min and max sizes. availableSize = Size.Fallback(availableSize).Clamp(MinSize, MaxSize); // Measure the size of child elements. var measuredSize = MeasureOverride(availableSize); measuredSize = Size.Fallback(measuredSize).Clamp(MinSize, MaxSize); measuredSize = measuredSize.Add(Margin); return measuredSize; }
protected override Size ArrangeOverride(Size finalSize) { Child?.Arrange(new Rectangle(BorderThickness + Padding, finalSize.Remove(BorderThickness).Remove(Padding))); return finalSize; }
/// <summary> /// Arranges the element. /// </summary> protected virtual void ArrangeCore(Rectangle finalRect) { // The final size will be equal to the desired size, unless horizontal or vertical alignment is stretch, in which case it will fill the container. var finalWidth = HorizontalAlignment != HorizontalAlignment.Stretch ? DesiredSize.Width : finalRect.Width; var finalHeight = VerticalAlignment != VerticalAlignment.Stretch ? DesiredSize.Height : finalRect.Height; var finalSize = new Size(finalWidth, finalHeight); finalSize = finalSize.Remove(Margin); // Remove the margin as it needs to be ignored. // If the elements width or height is 0 or NaN, fall back to the measured size. finalSize = Size.Fallback(finalSize).Clamp(MinSize, MaxSize); // Arrange all child elements and return a final size. var arrangedSize = ArrangeOverride(finalSize); // Get the offset for the element (due to alignment) var alignedOffset = Align(finalRect, arrangedSize.Add(Margin)); Position = alignedOffset + Margin; ActualSize = arrangedSize; }