protected virtual void ArrangeCore(Rect finalRect) { if (this.IsVisible) { double originX = finalRect.X + this.Margin.Left; double originY = finalRect.Y + this.Margin.Top; var size = new Size( Math.Max(0, finalRect.Width - this.Margin.Left - this.Margin.Right), Math.Max(0, finalRect.Height - this.Margin.Top - this.Margin.Bottom)); if (this.HorizontalAlignment != HorizontalAlignment.Stretch) { size = size.WithWidth(Math.Min(size.Width, this.DesiredSize.Value.Width)); } if (this.VerticalAlignment != VerticalAlignment.Stretch) { size = size.WithHeight(Math.Min(size.Height, this.DesiredSize.Value.Height)); } size = LayoutHelper.ApplyLayoutConstraints(this, size); size = this.ArrangeOverride(size).Constrain(size); switch (this.HorizontalAlignment) { case HorizontalAlignment.Center: originX += (finalRect.Width - size.Width) / 2; break; case HorizontalAlignment.Right: originX += finalRect.Width - size.Width; break; } switch (this.VerticalAlignment) { case VerticalAlignment.Center: originY += (finalRect.Height - size.Height) / 2; break; case VerticalAlignment.Bottom: originY += finalRect.Height - size.Height; break; } var bounds = new Rect(originX, originY, size.Width, size.Height); this.SetVisualBounds(bounds); this.SetValue(ActualSizeProperty, bounds.Size); } }
/// <summary> /// The default implementation of the control's measure pass. /// </summary> /// <param name="availableSize">The size available to the control.</param> /// <returns>The desired size for the control.</returns> /// <remarks> /// This method calls <see cref="MeasureOverride(Size)"/> which is probably the method you /// want to override in order to modify a control's arrangement. /// </remarks> protected virtual Size MeasureCore(Size availableSize) { if (IsVisible) { var margin = Margin; ApplyTemplate(); var constrained = LayoutHelper .ApplyLayoutConstraints(this, availableSize) .Deflate(margin); var measured = MeasureOverride(constrained); var width = measured.Width; var height = measured.Height; if (!double.IsNaN(Width)) { width = Width; } width = Math.Min(width, MaxWidth); width = Math.Max(width, MinWidth); if (!double.IsNaN(Height)) { height = Height; } height = Math.Min(height, MaxHeight); height = Math.Max(height, MinHeight); if (UseLayoutRounding) { var scale = GetLayoutScale(); width = Math.Ceiling(width * scale) / scale; height = Math.Ceiling(height * scale) / scale; } return(NonNegative(new Size(width, height).Inflate(margin))); } else { return(new Size()); } }
/// <summary> /// The default implementation of the control's measure pass. /// </summary> /// <param name="availableSize">The size available to the control.</param> /// <returns>The desired size for the control.</returns> /// <remarks> /// This method calls <see cref="MeasureOverride(Size)"/> which is probably the method you /// want to override in order to modify a control's arrangement. /// </remarks> protected virtual Size MeasureCore(Size availableSize) { if (IsVisible) { ApplyTemplate(); var constrained = LayoutHelper .ApplyLayoutConstraints(this, availableSize) .Deflate(Margin); var measured = MeasureOverride(constrained); var width = measured.Width; var height = measured.Height; if (!double.IsNaN(Width)) { width = Width; } width = Math.Min(width, MaxWidth); width = Math.Max(width, MinWidth); if (!double.IsNaN(Height)) { height = Height; } height = Math.Min(height, MaxHeight); height = Math.Max(height, MinHeight); return(new Size(width, height).Inflate(Margin)); } else { return(new Size()); } }
/// <summary> /// The default implementation of the control's arrange pass. /// </summary> /// <param name="finalRect">The control's new bounds.</param> /// <remarks> /// This method calls <see cref="ArrangeOverride(Size)"/> which is probably the method you /// want to override in order to modify a control's arrangement. /// </remarks> protected virtual void ArrangeCore(Rect finalRect) { if (IsVisible) { double originX = finalRect.X + Margin.Left; double originY = finalRect.Y + Margin.Top; var sizeMinusMargins = new Size( Math.Max(0, finalRect.Width - Margin.Left - Margin.Right), Math.Max(0, finalRect.Height - Margin.Top - Margin.Bottom)); var size = sizeMinusMargins; if (HorizontalAlignment != HorizontalAlignment.Stretch) { size = size.WithWidth(Math.Min(size.Width, DesiredSize.Width)); } if (VerticalAlignment != VerticalAlignment.Stretch) { size = size.WithHeight(Math.Min(size.Height, DesiredSize.Height)); } size = LayoutHelper.ApplyLayoutConstraints(this, size); if (UseLayoutRounding) { size = new Size( Math.Ceiling(size.Width), Math.Ceiling(size.Height)); sizeMinusMargins = new Size( Math.Ceiling(sizeMinusMargins.Width), Math.Ceiling(sizeMinusMargins.Height)); } size = ArrangeOverride(size).Constrain(size); switch (HorizontalAlignment) { case HorizontalAlignment.Center: case HorizontalAlignment.Stretch: originX += (sizeMinusMargins.Width - size.Width) / 2; break; case HorizontalAlignment.Right: originX += sizeMinusMargins.Width - size.Width; break; } switch (VerticalAlignment) { case VerticalAlignment.Center: case VerticalAlignment.Stretch: originY += (sizeMinusMargins.Height - size.Height) / 2; break; case VerticalAlignment.Bottom: originY += sizeMinusMargins.Height - size.Height; break; } if (UseLayoutRounding) { originX = Math.Floor(originX); originY = Math.Floor(originY); } Bounds = new Rect(originX, originY, size.Width, size.Height); } }
/// <summary> /// The default implementation of the control's arrange pass. /// </summary> /// <param name="finalRect">The control's new bounds.</param> /// <remarks> /// This method calls <see cref="ArrangeOverride(Size)"/> which is probably the method you /// want to override in order to modify a control's arrangement. /// </remarks> protected virtual void ArrangeCore(Rect finalRect) { if (IsVisible) { var margin = Margin; var originX = finalRect.X + margin.Left; var originY = finalRect.Y + margin.Top; var availableSizeMinusMargins = new Size( Math.Max(0, finalRect.Width - margin.Left - margin.Right), Math.Max(0, finalRect.Height - margin.Top - margin.Bottom)); var horizontalAlignment = HorizontalAlignment; var verticalAlignment = VerticalAlignment; var size = availableSizeMinusMargins; var scale = GetLayoutScale(); if (horizontalAlignment != HorizontalAlignment.Stretch) { size = size.WithWidth(Math.Min(size.Width, DesiredSize.Width - margin.Left - margin.Right)); } if (verticalAlignment != VerticalAlignment.Stretch) { size = size.WithHeight(Math.Min(size.Height, DesiredSize.Height - margin.Top - margin.Bottom)); } size = LayoutHelper.ApplyLayoutConstraints(this, size); if (UseLayoutRounding) { size = new Size( Math.Ceiling(size.Width * scale) / scale, Math.Ceiling(size.Height * scale) / scale); availableSizeMinusMargins = new Size( Math.Ceiling(availableSizeMinusMargins.Width * scale) / scale, Math.Ceiling(availableSizeMinusMargins.Height * scale) / scale); } size = ArrangeOverride(size).Constrain(size); switch (horizontalAlignment) { case HorizontalAlignment.Center: case HorizontalAlignment.Stretch: originX += (availableSizeMinusMargins.Width - size.Width) / 2; break; case HorizontalAlignment.Right: originX += availableSizeMinusMargins.Width - size.Width; break; } switch (verticalAlignment) { case VerticalAlignment.Center: case VerticalAlignment.Stretch: originY += (availableSizeMinusMargins.Height - size.Height) / 2; break; case VerticalAlignment.Bottom: originY += availableSizeMinusMargins.Height - size.Height; break; } if (UseLayoutRounding) { originX = Math.Floor(originX * scale) / scale; originY = Math.Floor(originY * scale) / scale; } Bounds = new Rect(originX, originY, size.Width, size.Height); } }