Grid 是 XAML 中最常用的版面配置容器之一。它先宣告列與欄,再把子元素放入指定儲存格。Grid 本身不負責顏色或樣式,只定義結構。
<Window
xmlns="https://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="https://schemas.microsoft.com/winfx/2006/xaml"
Title="Programming .Net 3.5 - Understanding Grids">
<Grid>
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<TextBlock FontSize="36" Foreground="White" Background="Blue"
Grid.Column="0" Grid.Row="0" Grid.RowSpan="2">1</TextBlock>
<TextBlock FontSize="36" Background="Gold"
Grid.Column="1" Grid.Row="0">2</TextBlock>
<TextBlock FontSize="36" Foreground="White" Background="Crimson"
Grid.Column="2" Grid.Row="0">3</TextBlock>
<TextBlock FontSize="36" Background="White"
Grid.Column="1" Grid.Row="1" Grid.ColumnSpan="2">4</TextBlock>
<TextBlock FontSize="36" Foreground="White" Background="Purple"
Grid.Column="0" Grid.Row="2">5</TextBlock>
<TextBlock FontSize="36" Foreground="White" Background="Green"
Grid.Column="1" Grid.Row="2">6</TextBlock>
<TextBlock FontSize="36" Foreground="White" Background="Black"
Grid.Column="2" Grid.Row="2">7</TextBlock>
</Grid>
</Window>
在 Kaxaml 工具中的呈現效果如下:

這個範例先定義一個 Grid,接著宣告三個 RowDefinition 與三個 ColumnDefinition,形成三列三欄的網格。之後每個 TextBlock 透過 Grid.Column 與 Grid.Row 指定所在位置。
屬性含義如下:
值得注意的是,Grid 本身沒有顏色,畫面上看到的色彩來自 TextBlock 等子元素。Grid 只提供結構定義,讓子元素在網格中排列。