37

I have the following setup on my WPF UserControl:

<GroupBox>
  <Grid>
    ...
    <Grid>
      <Grid.ColumnDefinitions>
        <ColumnDefinition Width="Auto" />

<GroupBox>
  <Grid>
    <Grid>
      <Grid.ColumnDefinitions>
        <ColumnDefinition Width="..." />

I'd like the second ColumnDefinition to be the same width as the first ColumnDefinition, but I don't want to set an explicit width. Instead, I want both grids columns to automatically stretch to the width of the longest piece of content in either grid column!

Is this possible?

devdigital
  • 34,151
  • 9
  • 98
  • 120

1 Answers1

85

It is possible by using SharedSizeGroup. Also check out IsSharedSizeScope.

<GroupBox Grid.IsSharedSizeScope="True">
  <Grid>
    ...
    <Grid>
      <Grid.ColumnDefinitions>
        <ColumnDefinition Width="Auto" SharedSizeGroup="A" />

<GroupBox>
  <Grid>
    <Grid>
      <Grid.ColumnDefinitions>
        <ColumnDefinition SharedSizeGroup="A" />

See here for more information.

Emond
  • 50,210
  • 11
  • 84
  • 115
Lars Truijens
  • 42,837
  • 6
  • 126
  • 143
  • 30
    Also ensure you set `Grid.IsSharedSizeScope="True"` on the outer container of the two grids, otherwise it won't work – Alex Jun 01 '16 at 13:31