Grid SharedSizeGroup - columns are bouncing, resizing in infinite loop

1.8k views Asked by At

I need table with both horizontal and vertical header (simple PivotGrid). I have found some similar (or almost same) problems here, but no one give the solution. In XAML I have defined this structure:

<Grid x:Name="grdMain" Background="White" Grid.IsSharedSizeScope="True">
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="Auto"/>
        <ColumnDefinition Width="Auto"/>
        <ColumnDefinition Width="Auto"/>
    </Grid.ColumnDefinitions>
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="Auto"/>
    </Grid.RowDefinitions>
    <Grid Grid.Row="0" Grid.Column="1" x:Name="grdHorizontalHeader">
        <!-- place for column definitions and header labels defined in code -->
    </Grid>
    <Grid Grid.Row="1" Grid.Column="0" x:Name="grdVerticalHeader">
        <!-- place for column definitions and header labels defined in code -->
    </Grid>
    <Grid Grid.Row="1" Grid.Column="1" x:Name="grdContent">
        <!-- place for column definitions and header labels defined in code -->
    </Grid>
</Grid>

So both header consist of grid with some ColumnDefinitions (resp. RowDefinitions) and I need to size Header-ColumnDefinitions according to Content-ColumnDefinitions. I do it in code:

foreach (var row in myColumnSource)
{

    // Content columns definitions

    var cD = new ColumnDefinition();
    cD.Width = GridLength.Auto;
    cD.SharedSizeGroup = "ColumnSharedSizeGroup" + row.Value;
    this.grdContent.ColumnDefinitions.Add(cD);

    // Header columns definitions

    var cD2 = new ColumnDefinition();
    cD2.Width = GridLength.Auto;
    cD2.SharedSizeGroup = "ColumnSharedSizeGroup" + row.Value;
    this.grdHorizontalHeader.ColumnDefinitions.Add(cD2);
    ...

So Header-Column should share it's Width with Content-Column. But when I run the program, the columns are bouncing and resizing in infinite loop. Row's height sharing work fine. Where could be the problem?

EDIT only columns in header (grdHorizontalHeader) are resizing. Columns in grdContent have correct and stable width.

2

There are 2 answers

0
Doug On

The Grid control's algorithm for auto-sizing can be finicky sometimes.

Have you tried setting a MinWidth on each of your columns/rows?

<Grid.ColumnDefinitions>
    <ColumnDefinition Width="Auto" MinWidth="30" />
    <ColumnDefinition Width="Auto" MinWidth="30" />
    <ColumnDefinition Width="Auto" MinWidth="30" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
    <RowDefinition Height="Auto" MinHeight="15" />
    <RowDefinition Height="Auto" MinHeight="15" />
    <RowDefinition Height="Auto" MinHeight="15" />
</Grid.RowDefinitions>

Not super elegant, but fixed this same problem for us.

0
AdamRossWalker On

If one grid has many SharedSizeGroup columns, the resulting layout performs an exponentially increasing number of passes to try and sort them out. Someone somewhere said Microsoft acknowledged the bug, but I couldn't find details of it.

To solve the issue, I broke my grid up into smaller grids inside a StackPanel. This made the layout fast again and retained the shared sizing.

As your grid is two dimensional, this would be harder. As you are adding controls dynamically, perhaps you could have one vertical StackPanel holding horizontal StackPanels holding smaller Grids?