public static Size ComputeDesiredSize(this IFrameworkElement frameworkElement, double widthConstraint, double heightConstraint) { _ = frameworkElement ?? throw new ArgumentNullException(nameof(frameworkElement)); if (frameworkElement.Handler == null) { return(Size.Zero); } var margin = frameworkElement.GetMargin(); // Adjust the constraints to account for the margins widthConstraint -= margin.HorizontalThickness; heightConstraint -= margin.VerticalThickness; // Determine whether the external constraints or the requested size values will determine the measurements widthConstraint = LayoutManager.ResolveConstraints(widthConstraint, frameworkElement.Width); heightConstraint = LayoutManager.ResolveConstraints(heightConstraint, frameworkElement.Height); // Ask the handler to do the actual measuring var measureWithMargins = frameworkElement.Handler.GetDesiredSize(widthConstraint, heightConstraint); // Account for the margins when reporting the desired size value return(new Size(measureWithMargins.Width + margin.HorizontalThickness, measureWithMargins.Height + margin.VerticalThickness)); }
public static Rectangle ComputeFrame(this IFrameworkElement frameworkElement, Rectangle bounds) { Thickness margin = frameworkElement.GetMargin(); // Normally the frame's width will be the element's desired width var frameWidth = frameworkElement.DesiredSize.Width; // But, if the element is set to fill horizontally and it doesn't have an explicitly set width, // the frame width will be large enough to fill the space if (frameworkElement.HorizontalLayoutAlignment == LayoutAlignment.Fill && frameworkElement.Width == -1) { frameWidth = Math.Max(0, bounds.Width - margin.HorizontalThickness); } // Normally the frame's height will be the element's desired height var frameHeight = frameworkElement.DesiredSize.Height; // But, if the element is set to fill vertically and it doesn't have an explicitly set height, // the frame height will be large enough to fill the space if (frameworkElement.VerticalLayoutAlignment == LayoutAlignment.Fill && frameworkElement.Height == -1) { frameHeight = Math.Max(0, bounds.Height - margin.VerticalThickness); } var frameX = AlignHorizontal(frameworkElement, bounds, margin); var frameY = AlignVertical(frameworkElement, bounds, margin); return(new Rectangle(frameX, frameY, frameWidth, frameHeight)); }
public static Rectangle ComputeFrame(this IFrameworkElement frameworkElement, Rectangle bounds) { Thickness margin = frameworkElement.GetMargin(); // We need to determine the width the element wants to consume; normally that's the element's DesiredSize.Width var consumedWidth = frameworkElement.DesiredSize.Width; if (frameworkElement.HorizontalLayoutAlignment == LayoutAlignment.Fill && frameworkElement.Width == -1) { // But if the element is set to fill horizontally and it doesn't have an explicitly set width, // then we want the width of the entire bounds consumedWidth = bounds.Width; } // And the actual frame width needs to subtract the margins var frameWidth = Math.Max(0, consumedWidth - margin.HorizontalThickness); // We need to determine the height the element wants to consume; normally that's the element's DesiredSize.Height var consumedHeight = frameworkElement.DesiredSize.Height; // But, if the element is set to fill vertically and it doesn't have an explicitly set height, // then we want the height of the entire bounds if (frameworkElement.VerticalLayoutAlignment == LayoutAlignment.Fill && frameworkElement.Height == -1) { consumedHeight = bounds.Height; } // And the actual frame height needs to subtract the margins var frameHeight = Math.Max(0, consumedHeight - margin.VerticalThickness); var frameX = AlignHorizontal(frameworkElement, bounds, margin); var frameY = AlignVertical(frameworkElement, bounds, margin); return(new Rectangle(frameX, frameY, frameWidth, frameHeight)); }
public static Rectangle ComputeFrame(this IFrameworkElement frameworkElement, Rectangle bounds) { Thickness margin = frameworkElement.GetMargin(); // If the margins are too big for the bounds, then simply collapse them to zero var frameWidth = Math.Max(0, bounds.Width - margin.HorizontalThickness); var frameHeight = Math.Max(0, bounds.Height - margin.VerticalThickness); return(new Rectangle(bounds.X + margin.Left, bounds.Y + margin.Top, frameWidth, frameHeight)); }
public static Rectangle ComputeFrame(this IFrameworkElement frameworkElement, Rectangle bounds) { Thickness margin = frameworkElement.GetMargin(); var frameWidth = frameworkElement.HorizontalLayoutAlignment == LayoutAlignment.Fill ? Math.Max(0, bounds.Width - margin.HorizontalThickness) : frameworkElement.DesiredSize.Width; var frameHeight = frameworkElement.VerticalLayoutAlignment == LayoutAlignment.Fill ? Math.Max(0, bounds.Height - margin.VerticalThickness) : frameworkElement.DesiredSize.Height; var frameX = AlignHorizontal(frameworkElement, bounds, margin); var frameY = AlignVertical(frameworkElement, bounds, margin); return(new Rectangle(frameX, frameY, frameWidth, frameHeight)); }