Search code examples
wpfgriduser-controlsstyling

WPF share column width between separate grids


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?


Solution

  • 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.