/// <summary> /// Updates DesiredSize of the ClipBorder. Called by parent UIElement. This is the first pass of layout. /// </summary> /// <remarks> /// Border determines its desired size it needs from the specified border the child: its sizing /// properties, margin, and requested size. /// </remarks> /// <param name="constraint">Constraint size is an "upper limit" that the return value should not exceed.</param> /// <returns>The Decorator's desired size.</returns> protected override Size MeasureOverride(Size constraint) { var child = Child; var desiredSize = new Size(); var borders = BorderThickness; // Compute the total size required var borderSize = borders.CollapseThickness(); var paddingSize = Padding.CollapseThickness(); // Does the ClipBorder have a child? if (child != null) { // Combine into total decorating size var combined = new Size(borderSize.Width + paddingSize.Width, borderSize.Height + paddingSize.Height); // Remove size of border only from child's reference size. var childConstraint = new Size(Math.Max(0.0, constraint.Width - combined.Width), Math.Max(0.0, constraint.Height - combined.Height)); child.Measure(childConstraint); var childSize = child.DesiredSize; // Now use the returned size to drive our size, by adding back the margins, etc. desiredSize.Width = childSize.Width + combined.Width; desiredSize.Height = childSize.Height + combined.Height; } else { // Since there is no child, the border requires only the size occupied by the BorderThickness // and the Padding desiredSize = new Size(borderSize.Width + paddingSize.Width, borderSize.Height + paddingSize.Height); } return desiredSize; }