WPF Grid Shared Size Groups


A WPF Grid contains a collection of rows and columns, which are sized explicitly,proportionately, or based on the size of their children. There’s one other way to size a row or a
column—to match the size of another row or column. This works through a feature called shared size groups.

The goal of shared size groups is to keep separate portions of your user interface consistent.For example, you might want to size one column to fit its content and size another column to
match that size exactly. However, the real benefit of shared size groups is to give the same proportions to separate Grid controls.

To understand how this works, consider the example shown in below Figure. This window features two Grid objects—one at the top of the window called Grid 1 (with three columns) and one at the bottom called Grid 2 (with two columns). The second column of the second grid(Grid 2)is linked with the first column of the second column of the first grid(Grid 1), if we resize  the column width of the first grid when the width of the second column also changes accordingly with first grid. The same way with the Hello text’s column.
second Grid stays synchronized.

Shared Grid Size

 <TextBlock Background=”Azure” Margin=”0″ VerticalAlignment=”Bottom”><Run Text=”Grid 1″/></TextBlock>
<Grid Background=”ForestGreen”  ShowGridLines=”True” Margin=”0,-3,0,0″ Grid.Row=”1″>
<Grid.ColumnDefinitions>
<ColumnDefinition Width=”Auto” SharedSizeGroup=”ShareThis”/>
<ColumnDefinition Width=”Auto” SharedSizeGroup=”MyCustom” MinWidth=”66.42″/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<TextBlock Width=”Auto” Foreground=”White” x:Name=”SharedTextBlock” Padding=”5″ Background=”#FF5B76AB” Text=”Ravi Srinivas”/>
<TextBlock Padding=”3″ Foreground=”White”  Grid.Column=”1″ Background=”#FFAB9D5B” Text=”Hello”/>
<TextBlock Padding=”3″ Foreground=”White”  Grid.Column=”2″/>
<GridSplitter Grid.Column=”1″
Width=”5″
ShowsPreview=”True” Background=”#FF1D5015″ ToolTip=”Drag me and see the Hello in Second Grid”/>
<GridSplitter Grid.Column=”0″ Width=”5″ Background=”#FF160505″ ToolTip=”Drag me and see the Ravi Srinivas in Second Grid” />
</Grid>
<TextBlock Background=”Azure”  Grid.Row=”2″ Margin=”0,16,0,0″><Run Text=”Grid 2″/></TextBlock>
<Grid Grid.Row=”3″ Background=”ForestGreen”  ShowGridLines=”True”>
<Grid.ColumnDefinitions>
<ColumnDefinition Width=”Auto” SharedSizeGroup=”MyCustom”/>
<ColumnDefinition Width=”Auto” SharedSizeGroup=”ShareThis” />
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<TextBlock Foreground=”White”  Width=”Auto” x:Name=”SharedTextBlock2″ Background=”#FFAB9D5B” Text=”Hello”/>
<TextBlock Foreground=”White”  Grid.Column=”1″ Background=”#FF5B76AB” Text=”Ravi Srinivas”/>
<GridSplitter Grid.Column=”1″ Width=”5″ Background=”#FF160505″ ToolTip=”Drag me and see the Hello in First Grid” HorizontalAlignment=”Left” Margin=”0″ />
</Grid>

Download the sample here

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s