#457 – Use a GridSplitter to Let a User Change Row or Column Size in a Grid
December 23, 2011 2 Comments
Rows and columns in a Grid don’t normally change their size at runtime, unless the content of the Grid changes. But you can allow a user to change the size of rows or columns by using a GridSplitter element.
You typically place a GridSplitter in its own row or column. In the example below, the middle column contains a blue GridSplitter. At runtime, a user can click and drag on this element to change the relative sizes of columns 0 and 2.
<Grid>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Label Content="Row 0 Col 0" Background="Azure" Grid.Row="0" Grid.Column="0" />
<Label Content="Row 0 Col 2" Background="Lavender" Grid.Row="0" Grid.Column="2" />
<Label Content="Row 1 Col 0" Background="Moccasin" Grid.Row="1" Grid.Column="0" />
<Label Content="Row 1 Col 2" Background="Honeydew" Grid.Row="1" Grid.Column="2" />
<GridSplitter Grid.Column="1" Grid.RowSpan="2" Width="3" Background="Blue"
VerticalAlignment="Stretch" HorizontalAlignment="Center" />
</Grid>
Layout at startup:

User clicks and drags to the left:

Or drags to the right:

Pingback: #459 – Using Two (or More) GridSplitter Elements in the Same Grid « 2,000 Things You Should Know About WPF
Pingback: #466 – Using a GridSplitter in Conjunction with a SharedSizeGroup « 2,000 Things You Should Know About WPF