#425 – SharedSizeGroup Allows Sharing Column Sizes Across Different Grids

You can use the SharedSizeGroup property of a ColumnDefinition to lead to consistent column sizes in a ListBox with a data template.  This works because each entry in the list has its own instance of a Grid and the column sizes are being shared across different grids.

As a more general example, we can share column sizes across two Grid controls hosted in the same window.

    <StackPanel Grid.IsSharedSizeScope="True">
        <Label Content="First grid:" />
        <Grid Margin="10" ShowGridLines="True" Background="AliceBlue">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="Auto" SharedSizeGroup="A"/>
                <ColumnDefinition/>
                <ColumnDefinition/>
            </Grid.ColumnDefinitions>

            <Label Grid.Column="0" Content="{Binding Thing1}"/>
        </Grid>

        <Label Content="Second grid:" />
        <Grid Margin="10" ShowGridLines="True" Background="AliceBlue">
            <Grid.ColumnDefinitions>
                <ColumnDefinition/>
                <ColumnDefinition Width="Auto" SharedSizeGroup="A"/>
                <ColumnDefinition/>
            </Grid.ColumnDefinitions>

            <Label Grid.Column="1" Content="{Binding Thing2}"/>
        </Grid>

        <Button Margin="10" Content="Change Thing1" Click="Button_Click" />
    </StackPanel>

We share size between the first column in the first grid and the second column in the second grid, since they use the same value for SharedSizeGroup.

After adding characters to Thing1:

About Sean
Software developer in the Twin Cities area, passionate about .NET technologies. Equally passionate about my own personal projects related to family history and preservation of family stories and photos.

3 Responses to #425 – SharedSizeGroup Allows Sharing Column Sizes Across Different Grids

  1. Pingback: Dew Drop – November 8, 2011 | Alvin Ashcraft's Morning Dew

  2. Pingback: #466 – Using a GridSplitter in Conjunction with a SharedSizeGroup « 2,000 Things You Should Know About WPF

  3. psulek says:

    Thanks a lot for this sample! Important for me was to have 2 grids within same parent StackPanel and set Grid.IsSharedSizeScope=”True” directly on that parent StackPanel. I have the wrong way on both Grids and it does not share sizes. Now it works well!

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

%d bloggers like this: