Module 1 · Lesson 03 of 16
Star sizing, and nothing else
Layout is measure and arrange. Grid star units share what is left, and one alignment mistake collapses them to nothing.
Avalonia does not position controls with coordinates. It asks each control how much
space it wants (Measure), then tells it how much it gets (Arrange). Every
surprising layout result is one of those two phases disagreeing with your mental
model.
The grid in the sample
<Grid x:Name="DemoGrid" ColumnDefinitions="140,*,2*" RowDefinitions="64,*" Height="300">
<Border Grid.Column="0" Grid.RowSpan="2" Background="{DynamicResource SurfaceMuted}"
BorderBrush="{DynamicResource Rule}" BorderThickness="1" CornerRadius="4">
<StackPanel VerticalAlignment="Center" HorizontalAlignment="Center" Spacing="4">
<TextBlock Text="140" FontWeight="SemiBold" HorizontalAlignment="Center" />
<TextBlock Classes="caption" Text="fixed" HorizontalAlignment="Center" />
</StackPanel>
</Border>
<Border Grid.Column="1" Grid.ColumnSpan="2" Margin="8,0,0,0"
Background="{DynamicResource AccentSoft}"
BorderBrush="{DynamicResource Rule}" BorderThickness="1" CornerRadius="4">
<TextBlock Text="Auto-height header" VerticalAlignment="Center" Margin="14,0" />
</Border>
<Border Grid.Column="1" Grid.Row="1" Margin="8,8,0,0"
Background="{DynamicResource SurfaceMuted}"
BorderBrush="{DynamicResource Rule}" BorderThickness="1" CornerRadius="4">
<StackPanel VerticalAlignment="Center" HorizontalAlignment="Center" Spacing="4">
<TextBlock Text="*" FontWeight="SemiBold" HorizontalAlignment="Center" />
<TextBlock Classes="caption" Text="one share" HorizontalAlignment="Center" />
</StackPanel>
</Border>
<Border Grid.Column="2" Grid.Row="1" Margin="8,8,0,0"
Background="{DynamicResource SurfaceMuted}"
BorderBrush="{DynamicResource Rule}" BorderThickness="1" CornerRadius="4">
<StackPanel VerticalAlignment="Center" HorizontalAlignment="Center" Spacing="4">
<TextBlock Text="2*" FontWeight="SemiBold" HorizontalAlignment="Center" />
<TextBlock Classes="caption" Text="two shares" HorizontalAlignment="Center" />
</StackPanel>
</Border>
</Grid>
</Border>
<TextBlock Classes="caption" MaxWidth="760" HorizontalAlignment="Left" TextWrapping="Wrap"
Text="Column widths here are 140, 1 part, and 2 parts of the remainder. Resize the window: the fixed column holds, the other two keep their ratio. Margin is outside the border, Padding is inside — mixing them up is the most common layout bug." />
ColumnDefinitions="140,*,2*" means: 140 pixels, then one share, then two shares
of whatever remains. The header row is Auto-sized by its content; the body row
takes the rest.
The properties that actually decide the result are the boring ones:
- Margin is outside the control's border. Padding is inside it.
- HorizontalAlignment defaults to
Stretch. SettingLeftmakes the control size to its content — which is exactly how star columns collapse. - MinWidth/MaxWidth constrain the measure pass, not the arrange pass.
The bug this page shipped with. The demo grid was wrapped in a border with
HorizontalAlignment="Left". Star columns inside an auto-sized container have no remainder to divide, so they rendered at zero. The assertion caught it — the screenshot alone looked plausible.
When to reach for what
| Need | Use |
|---|---|
| A form with labels and inputs | Grid with Auto label column and * input column |
| A toolbar | StackPanel with Orientation="Horizontal" |
| A sidebar and content | Grid ColumnDefinitions="260,*" |
| Content that can exceed the window | ScrollViewer around the page |
| Long lists | ListBox / ItemsControl, never a StackPanel of 1,000 rows |
ScrollViewer measures its child with infinite height. Anything inside it that
expects to fill the viewport (* rows, VerticalAlignment="Stretch") will instead
size to content. That is not a bug; it is the contract. Use a Grid at the page
root when you need both scrolling and a fixed footer.
Try it
- Add a third body row with
Height="Auto"and a caption. Watch it take only what it needs. - Change
2*toAuto. The right column now sizes to its text and the middle column claims the remainder. - Resize the window. Column 0 never moves; the others keep their ratio.
Next: lists that stay in sync.
Files this lesson draws from
src/CourseApp/Lessons/03_Layout/LayoutView.axaml
VerifiedThe verifier hosts the page in a real window and asserts the fixed column is 140px while the two star columns hold a 1:2 ratio. The first version of this page failed that check.