Grid and StackPanel Overview
Grid and StackPanel are the two panels you will reach for most often in everyday WPF development. Grid divides available space into a matrix of rows and columns defined via RowDefinitions and ColumnDefinitions, and each child is placed into a specific cell using the attached properties Grid.Row and Grid.Column, with Grid.RowSpan and Grid.ColumnSpan allowing a child to occupy multiple cells. StackPanel, by contrast, has no concept of cells at all — it simply lays children out one after another along a single axis, controlled by its Orientation property, which defaults to Vertical. Because Grid gives you precise two-dimensional control while StackPanel gives you simple one-dimensional flow, most real applications combine both: a Grid for the overall page skeleton, and StackPanel for smaller linear groupings like a button bar within one of the Grid's cells.
Cricket analogy: A scorecard organizes runs into a strict rows-and-columns table for each batsman per over, similar to how a Grid places elements into precise Grid.Row and Grid.Column cells.
Grid: Rows, Columns, and Cell Placement
Grid supports three sizing modes for rows and columns: Auto, which sizes to the content's natural size; a fixed pixel value, like Width="100"; and star sizing (denoted with *), which distributes remaining space proportionally after Auto and fixed-size rows/columns have been allocated. For instance, a ColumnDefinitions list of Auto, *, 2* gives the first column exactly as much width as its content needs, then splits the remaining space so the third column gets twice as much as the second. Grid also supports a ShowGridLines debugging property and GridSplitter controls that let the end user drag to resize adjacent rows or columns interactively, which is common in tool-window-style applications like a code editor's file explorer next to the editing pane.
Cricket analogy: Allocating a T20 innings' 20 overs — a fixed powerplay of 6 overs (fixed size) then splitting the rest proportionally between death and middle overs (star sizing) — mirrors how Grid allocates Auto, fixed, and star-sized rows.
<Grid ShowGridLines="False">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="2*" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<TextBlock Grid.Row="0" Grid.Column="0" Text="Label:" VerticalAlignment="Center" />
<TextBox Grid.Row="0" Grid.Column="1" Grid.ColumnSpan="2" Margin="4" />
<ListBox Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="1" Width="150" />
<GridSplitter Grid.Row="1" Grid.Column="1" Width="4" HorizontalAlignment="Left" />
<Border Grid.Row="1" Grid.Column="1" Grid.ColumnSpan="2" Background="White" />
</Grid>StackPanel: Sequential Layout
StackPanel measures each child with essentially infinite space along its stacking axis and gives each child its natural DesiredSize along that axis, while constraining the cross-axis to the StackPanel's own available width (for Vertical orientation) or height (for Horizontal orientation). This makes StackPanel extremely simple to reason about but also means it does not truncate or wrap overflow content — if the combined height of a vertical StackPanel's children exceeds the visible area, the extra content is simply clipped or pushed off-screen unless the StackPanel is itself hosted inside a ScrollViewer. A very common pattern is a horizontal StackPanel used as a button bar at the bottom of a dialog, where each Button's Margin provides spacing between siblings since StackPanel itself has no built-in item-spacing property.
Cricket analogy: A batting order lists batsmen one after another without regard for how many are left in the shed — if the innings runs long, the tail simply doesn't get to bat — just as StackPanel content that overflows is clipped rather than reflowed.
StackPanel does not provide item spacing directly (unlike some newer UI frameworks). Use a consistent Margin on each child, or apply a Style with a Setter on the Margin property targeting the panel's child type, to keep spacing uniform without repeating Margin on every element.
Combining Grid and StackPanel
The idiomatic WPF pattern is to use Grid for the outer skeleton of a view — separating a header, a main content area, and a footer into rows, or a navigation rail and content pane into columns — and then drop a StackPanel into individual Grid cells wherever you need a simple linear grouping, such as a vertical stack of form fields or a horizontal row of action buttons. Because Grid.Row and Grid.Column are attached properties, a StackPanel placed inside a Grid simply sets Grid.Row="1" on itself; it does not need to know anything about the Grid's row/column definitions beyond that. This separation of concerns — Grid for macro-structure, StackPanel for micro-structure — keeps XAML readable and avoids using Grid's more verbose row/column syntax for trivial single-axis groupings.
Cricket analogy: A team's overall strategy board (Grid) splits the day into session-by-session cells, while within one session the bowling rotation (StackPanel) is just a simple sequential list, combining macro and micro structure.
A common bug is forgetting that a StackPanel gives its children unconstrained space along the stacking axis, which means percentage-based or star-based sizing (Height="*") on a child inside a StackPanel has no effect — star sizing only works inside a Grid's row/column definitions.
- Grid organizes children into rows and columns using RowDefinitions/ColumnDefinitions and the Grid.Row/Grid.Column attached properties.
- Grid supports Auto, fixed, and star (*) sizing, letting remaining space be distributed proportionally.
- GridSplitter allows end users to interactively resize adjacent rows or columns.
- StackPanel arranges children sequentially along a single axis controlled by Orientation.
- StackPanel gives children unconstrained space along the stacking axis, so star sizing does not apply inside it.
- StackPanel has no built-in item spacing; use Margin or a Style Setter for consistent gaps.
- The idiomatic pattern nests StackPanel inside Grid cells: Grid for macro-structure, StackPanel for micro-structure.
Practice what you learned
1. What does a ColumnDefinition Width of '2*' mean relative to a sibling column with Width='*'?
2. Which attached property lets a child span multiple Grid rows?
3. Why doesn't setting Height='*' on a child inside a StackPanel have any effect?
4. What control lets an end user interactively resize adjacent Grid rows or columns?
5. What happens to a vertical StackPanel's content when it exceeds the visible area and there is no ScrollViewer?
Was this page helpful?
You May Also Like
Layout Panels Overview
An introduction to how WPF's layout system works and how panel classes cooperate to size and position UI elements.
Canvas and DockPanel
How Canvas provides absolute pixel positioning and DockPanel anchors children to container edges, and when to use each.
Alignment and Margins
How HorizontalAlignment, VerticalAlignment, Margin, and Padding control an element's position and spacing inside its allocated layout slot.
Related Reading
Related Study Notes in Microsoft Technologies
Browse all study notesWindows 10 / UWP Development Study Notes
.NET · 30 topics
Microsoft TechnologiesWindows Batch Scripting Study Notes
Batch · 30 topics
Microsoft TechnologiesMFC (Microsoft Foundation Classes) Study Notes
C++ · 30 topics
Microsoft TechnologiesSilverlight Study Notes
.NET · 30 topics
Microsoft TechnologiesXAML Study Notes
.NET · 30 topics
Microsoft TechnologiesMVVM Design Pattern Study Notes
.NET · 30 topics