public override sw.Size GetPreferredSize(sw.Size constraint) { var size = PreferredSize; if (double.IsNaN(size.Width) || double.IsNaN(size.Height)) { sw.Size baseSize; if (UseContentSize) { var padding = border.Padding.Size().Add(ContainerControl.Margin.Size()); var contentSize = constraint.Subtract(padding); var preferredSize = content.GetPreferredSize(contentSize); baseSize = new sw.Size(Math.Max(0, preferredSize.Width + padding.Width), Math.Max(0, preferredSize.Height + padding.Height)); } else { baseSize = base.GetPreferredSize(constraint); } if (double.IsNaN(size.Width)) { size.Width = baseSize.Width; } if (double.IsNaN(size.Height)) { size.Height = baseSize.Height; } } return(new sw.Size(Math.Max(0, size.Width), Math.Max(0, size.Height))); }
public override sw.Size GetPreferredSize(sw.Size constraint) { var size = PreferredSize; var margin = ContainerControl.Margin.Size(); if (double.IsNaN(size.Width) || double.IsNaN(size.Height)) { sw.Size baseSize; if (UseContentSize) { var padding = border.Padding.Size(); var childConstraint = constraint.Subtract(padding).Subtract(margin); baseSize = content.GetPreferredSize(childConstraint); baseSize = baseSize.Add(padding); // we add margin back at end } else { baseSize = base.GetPreferredSize(constraint).Subtract(margin); } size = size.IfNaN(baseSize); } size = size.Max(ContainerControl.GetMinSize()); size = size.Add(margin); return(size); }
public override sw.Size GetPreferredSize(sw.Size constraint) { if (columnScale == null || columnScale.Length == 0 || rowScale == null || rowScale.Length == 0) { return(ContainerControl.GetMinSize().Add(ContainerControl.Margin.Size())); } var margin = ContainerControl.Margin.Size(); var size = PreferredSize; if (double.IsNaN(size.Width) || double.IsNaN(size.Height)) { var padding = border.Padding.Size(); var widths = new double[columnScale.Length]; var heights = new double[rowScale.Length]; var scaledCells = new Size(); var childConstraint = constraint.Subtract(padding); childConstraint = childConstraint.Subtract(margin); // check non-scaled rows/columns first with preferred size for (int y = 0; y < rowScale.Length; y++) { var cellConstraint = childConstraint; var hasRowScale = IsRowScaled(y); if (hasRowScale) { scaledCells.Height++; } else { cellConstraint.Height = double.PositiveInfinity; } for (int x = 0; x < widths.Length; x++) { var hasColScale = IsColumnScaled(x); if (hasColScale && y == 0) { scaledCells.Width++; } if (!hasColScale) { cellConstraint.Width = double.PositiveInfinity; } if (!hasColScale || !hasRowScale) { var control = controls[x, y]; var childControl = control.GetWpfFrameworkElement(); if (childControl != null && control.Visible) { var preferredSize = childControl.GetPreferredSize(cellConstraint); if (!hasColScale) { widths[x] = Math.Max(widths[x], preferredSize.Width); } if (!hasRowScale) { heights[y] = Math.Max(heights[y], preferredSize.Height); } } else { var cellMargins = GetMargins(x, y); widths[x] = Math.Max(widths[x], cellMargins.Horizontal()); heights[y] = Math.Max(heights[y], cellMargins.Vertical()); } } } } // find remaning size for each cell childConstraint = childConstraint.Subtract(new sw.Size(widths.Sum(), heights.Sum())); childConstraint.Width /= scaledCells.Width; childConstraint.Height /= scaledCells.Height; // check scaled rows/columns based on remaining size for (int y = 0; y < rowScale.Length; y++) { var cellConstraint = childConstraint; var hasRowScale = IsRowScaled(y); if (!hasRowScale) { cellConstraint.Height = double.PositiveInfinity; } for (int x = 0; x < widths.Length; x++) { var hasColScale = IsColumnScaled(x); if (!hasColScale) { cellConstraint.Width = double.PositiveInfinity; } if (hasColScale || hasRowScale) { var control = controls[x, y]; var childControl = control.GetWpfFrameworkElement(); if (childControl != null && control.Visible) { var preferredSize = childControl.GetPreferredSize(cellConstraint); widths[x] = Math.Max(widths[x], preferredSize.Width); heights[y] = Math.Max(heights[y], preferredSize.Height); } else { var cellMargins = GetMargins(x, y); widths[x] = Math.Max(widths[x], cellMargins.Horizontal()); heights[y] = Math.Max(heights[y], cellMargins.Vertical()); } } } } var preferredContentSize = new sw.Size(widths.Sum(), heights.Sum()); preferredContentSize = preferredContentSize.Add(padding); size = size.IfNaN(preferredContentSize); } size = size.Max(ContainerControl.GetMinSize()); size = size.Add(margin); //System.Diagnostics.Debug.WriteLine("Size: {0}, actual: {1}", size, ContainerControl.GetSize()); return(size); }