Layoutable
TheLayoutable class implements layout-related functionality for visual controls in Avalonia. It’s the base class for all controls that participate in the layout system.
Namespace
Inheritance
Enumerations
HorizontalAlignment
The control stretches to fill the width of the parent.
The control aligns to the left of the parent.
The control centers itself horizontally in the parent.
The control aligns to the right of the parent.
VerticalAlignment
The control stretches to fill the height of the parent.
The control aligns to the top of the parent.
The control centers itself vertically in the parent.
The control aligns to the bottom of the parent.
Properties
Gets or sets the width of the control. Default is
double.NaN (auto-size).Gets or sets the height of the control. Default is
double.NaN (auto-size).Gets or sets the minimum width of the control. Default is 0.
Gets or sets the minimum height of the control. Default is 0.
Gets or sets the maximum width of the control. Default is
double.PositiveInfinity.Gets or sets the maximum height of the control. Default is
double.PositiveInfinity.Gets or sets the margin around the control.Margin is space outside the control that separates it from adjacent controls.
Gets or sets how the control aligns itself horizontally in its parent. Default is
Stretch.Gets or sets how the control aligns itself vertically in its parent. Default is
Stretch.Gets the size that this control computed during the measure pass.This is a read-only property set by the
Measure method.Gets or sets whether layout rounding is enabled. Default is
true.Layout rounding snaps layout to pixel boundaries for crisp rendering.Methods
Measures the control and its children.Parameters:
availableSize(Size): The available size for the control
Arranges the control and its children.Parameters:
rect(Rect): The final size and position for the control
Invalidates the measurement state of the control.Call this when a property changes that affects the control’s desired size.
Invalidates the arrangement state of the control.Call this when a property changes that affects how children are arranged.
Events
Occurs when the control’s effective viewport changes.Useful for implementing virtualization.
Occurs when a layout pass completes for the control.
Usage Examples
Setting Size Properties
Alignment
Margin
Code-Behind
Custom Layout
Layout Process
The layout process consists of two passes:- Measure Pass: Controls calculate their desired size based on available space
- Arrange Pass: Controls are positioned and given their final size
Best Practices
Prefer auto-sizing over fixed sizes
Prefer auto-sizing over fixed sizes
Use
Width and Height only when necessary. Let controls size themselves based on content.Use Min/Max instead of fixed Width/Height
Use Min/Max instead of fixed Width/Height
Constraints allow flexibility while preventing extreme sizes.
Consider panel Spacing instead of Margin
Consider panel Spacing instead of Margin
Modern panels like StackPanel have Spacing properties that are cleaner than individual margins.