Noel Kennedy
Noel Kennedy

Reputation: 12268

Using SharedSizeGroup with ColumnSpan in a Wpf Grid

When I create a grid using both a SharedSizeGroup on columns and a column spanning control, the grid goes 'mental' jerking around and maxing out a cpu core.

I'm sure there must be a good reason why this doesn't work but I can't think of it! How else can I achieve this sizing layout?

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

        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
        </Grid.RowDefinitions>

        <Label Grid.Column="0">Blah</Label>
        <Label Grid.Column="1">Blah Blah Blah Blah</Label>

        <Label Grid.Row="1" Grid.ColumnSpan="2">ajsgdeererajgsfdg dfg df gdfg djgsad</Label>
    </Grid>

Upvotes: 3

Views: 2373

Answers (1)

John Bowen
John Bowen

Reputation: 24453

What you've done is essentially set up infinite recursion in your layout.

  • To share sizing the Columns first need to calculate their own size.
  • They can then sync up based on the larger one (probably Column 1 here).
  • After Column 0 expands to match Column 1's size, the third label can now use more of the space in Column
  • As it shifts columns, the space it needs in Column 1 becomes smaller, allowing Column 1 to scale down to match the new space required by its contents.
  • Column 1 now recalculates its size (start over at the first step)

SharedSizeGroup is intended to be used across different Grids to maintain alignment of elements that are somehow separated into different containers, like different templated list items or a header row. If you need an equally split row that isn't stretched you could use something else like * sized columns or a UniformGrid instead.

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="Auto"/>
    </Grid.RowDefinitions>

    <UniformGrid Rows="1" HorizontalAlignment="Left">
        <Label >Blah</Label>
        <Label >Blah Blah Blah Blah</Label>
    </UniformGrid>

    <Label Grid.Row="1">ajsgdeererajgsfdg dfg df gdfg djgsad</Label>
</Grid>

Upvotes: 4

Related Questions