Grid Control
The Grid
panel arranges child elements in rows and columns, providing a flexible layout system for WinUI applications.
Definition
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="2*"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
</Grid>
Properties
Property | Type | Description |
---|---|---|
RowDefinitions | RowDefinitionCollection | Defines the rows of the grid. |
ColumnDefinitions | ColumnDefinitionCollection | Defines the columns of the grid. |
ShowGridLines | bool | Shows the grid lines at runtime (useful for debugging). |
Background | Brush | Background brush for the panel. |
Methods
InvalidateArrange()
– Forces a layout pass.InvalidateMeasure()
– Forces a measurement pass.
Examples
XAML
C# Code‑Behind
<Grid ShowGridLines="True">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="2*"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<TextBlock Text="Header" Grid.Row="0" Grid.ColumnSpan="2"
FontSize="24" HorizontalAlignment="Center"/>
<ListView Grid.Row="1" Grid.Column="0"/>
<StackPanel Grid.Row="1" Grid.Column="1" Margin="8">
<Button Content="Add"/>
<Button Content="Remove" Margin="0,8,0,0"/>
</StackPanel>
</Grid>
Remarks
Use Auto
, *
(star sizing), or a fixed value for row and column sizes. Star sizing distributes remaining space proportionally.
Nested grids are common for complex layouts. Keep the visual tree shallow when possible to improve performance.