devdigital
devdigital

Reputation: 34349

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?

Upvotes: 37

Views: 24655

Answers (1)

Lars Truijens
Lars Truijens

Reputation: 43602

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.

Upvotes: 86

Related Questions