0
votes

I am having an issue with column sizing in a WPF datagrid.

Below I have an example with 2 equal datagrids. Both have 1 fixed column (Code) and 1 column set the fill with * sizing (Description). They are both contained within a layout grid.

The first datagrid is contained within a fixed sized column (WorkingGrid) in the layout grid. It works fine. The fixed "Code" column is the correct width and the * sized "Description" one fills the remaining space.

The second datagrid is contained within an auto sized column (BrokenGrid) in the layout grid. It has exactly the same setup as the other datagrid yet ignores all datagrid column widths specified. They appear to become the default minimum size, which I think is 20. The strange thing is, the actual datagrid itself expands to fill up the remaining space and so do all other controls and layout grids that sit in that BrokenGrid column. The Datagrid columns remain squashed down to 20px, including the fixed "Code" one which had a width of 100 specified.

Here is the example, simplified greatly:

<Grid Attached:ReadOnlyOptions.IsReadOnly="{Binding IsReadOnly}">
    <Grid.ColumnDefinitions>
        <ColumnDefinition x:Name="WorkingGrid" Width="500"/>
        <ColumnDefinition x:Name="BrokenGrid" Width="Auto"/>
    </Grid.ColumnDefinitions>

    <DataGrid Grid.Column="0" VerticalAlignment="Top" ItemsSource="{Binding AllResults}">
        <DataGrid.Columns>
            <DataGridTextColumn Header="code" Width="100" Binding="{Binding Path=Code}"/>
            <DataGridTextColumn Header="description" Width="*" Binding="{Binding Path=Description}"/>
        </DataGrid.Columns>
    </DataGrid>

    <DataGrid Grid.Column="1" VerticalAlignment="Top" ItemsSource="{Binding AllResults}">
        <DataGrid.Columns>
            <DataGridTextColumn Header="code" Width="100" Binding="{Binding Path=Code}"/>
            <DataGridTextColumn Header="description" Width="*" Binding="{Binding Path=Description}"/>
        </DataGrid.Columns>
    </DataGrid>
</Grid>

In the real world example there is actually a large hierarchy of grids within grids, but if any of the columns (that directly or indirectly contain the datagrid) in any of those grids the whole way up the hierarchy chain has a width of auto, it breaks ONLY the datagrid columns. If I fix or * the width of those columns, the datagrid columns behave correctly.

I need this to work so I can have a set of controls/datagrids contained within a grid that all automatically expand to the width of the widest content, but all datagrids within the set have a column that fills that datagrid's width (minus whatever fixed widths columns are in there).

1

1 Answers

0
votes

If it possible use a Width="*" for BrokenGrid.

The second solution is set fix width for second DataGrid


The difference between "*" and "Auto":

  • "*" - takes all left place (no matter how much element in column needs)
  • "Auto" - takes all left place or less (as much as element in column needs)

In your case, DataGrid tells BrokenGrid, that its needs 42.0px (I think i could be some DataGrid bug, it should wants at least 100px)