0

I have a WPF DataGrid and no matter what I try it won't resize to fit the available space. In my Window there is a lot of empty space to the right and I want the columns to change their width according to the space available.

The code I have for the Datagrid is:

<DataGrid x:Name="ResultDataGrid" Margin="40,30,6,6" Grid.Row="1"  ScrollViewer.CanContentScroll="True" AutoGenerateColumns="False" HorizontalContentAlignment="Stretch" VerticalContentAlignment="Stretch"
              ItemsSource="{Binding Records}" Background="Transparent"   HeadersVisibility="Column" IsReadOnly="true" CanUserSortColumns="False"  LostFocus="ResultDataGrid_LostFocus" HorizontalAlignment="Left"  VerticalAlignment="Top">

        <DataGrid.ColumnHeaderStyle>
            <Style TargetType="DataGridColumnHeader">
                <EventSetter Event="Click" Handler="ColumnHeader_Click" />
            </Style>
        </DataGrid.ColumnHeaderStyle>

        <DataGrid.Columns>
            <DataGridTemplateColumn>
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <CheckBox VerticalAlignment="Center" IsChecked="{Binding Path=IsCheck,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"  Click="CheckBox_Click"/>
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>
            <DataGridTextColumn x:Name="DeviceSNDataGridText" Binding="{Binding InsCCode}" 
                                Header="{StaticResource DeviceSNString}"  FontSize="14"/>
            <DataGridTextColumn x:Name="DataTimeDataGridText" Binding="{Binding Created}" 
                                Header="{StaticResource DateTimeString}"/>
            <DataGridTextColumn x:Name="ModeDataGridText" Binding="{Binding Mode}" 
                                Header="{StaticResource ModeString}"  FontSize="14" />
            <DataGridTextColumn x:Name="ResultDataGridText" Binding="{Binding Result}" 
                                Header="{StaticResource ResultString}"  FontSize="14"  />
            <DataGridTextColumn x:Name="ScanIndexDataGridText" Binding="{Binding ScanIndex}"
                                Header="{StaticResource ScanIndexString}"  FontSize="14" />
        </DataGrid.Columns>
    </DataGrid>

After reading online, I tried setting each DataGridTextColumn width to * but that gave me this resulting datagrid below:

enter image description here

I also tried setting the ColumnWidth on the Datagrid itself to * but it also gave the same result as the picture above.

By the way, my datagrid is contained within a grid (on Row 1) which is defined like this:

<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition/>
    </Grid.ColumnDefinitions>
    <Grid.RowDefinitions>
        <RowDefinition Height="70"/>
        <RowDefinition Height="*"/>
        <RowDefinition Height="auto"/>
        <RowDefinition Height="auto"/>
        <RowDefinition Height="auto"/>
        <RowDefinition Height="50"/>
        <RowDefinition Height="20"/>
    </Grid.RowDefinitions>

What am I missing? Any suggestions please?

Kafka
  • 21
  • 2
  • Try without the HorizontalAlignment – the.Doc Nov 30 '21 at 22:33
  • @the.Doc I tried that before but that would just give me an empty column at the end which will take up all available space. I don't want that. That's why I have HorizontalAlignment="Left" – Kafka Nov 30 '21 at 22:40
  • Ok, in that case set the column widths to Auto instead of * – the.Doc Nov 30 '21 at 22:44
  • @the.Doc I tried that already. "Auto means that a column is given as much width as the elements within it require." That will not fix the issue. – Kafka Nov 30 '21 at 22:49
  • Do you want the columns to fill the available width and be spaced evenly? If so, remove the HorizontalAlignment and use * sizing on all columns. If you want each column to size to its content and be left aligned, keep the HorizontalAlignment and use Auto sizing. – the.Doc Nov 30 '21 at 23:08
  • @the.Doc Yes. I want the columns to fill the available width and be spaced evenly. But, when I remove HorizontalAlignment and set each column width to * , the columns all become squished together like the picture above. The only difference is that there is an empty column at the end which takes up all the available space. – Kafka Dec 01 '21 at 14:04
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/239734/discussion-between-kafka-and-the-doc). – Kafka Dec 01 '21 at 14:10
  • 1
    @Kafka: Could you please provide a full reproducible example including the a parent window? – mm8 Dec 01 '21 at 20:56
  • Please create a minimal example that reproduces the issue. This way you may be able to solve the issue while creating the example. What you want is the default behavior. So a parent control must change the layout behavior. If you don't provide this minimal example, nobody can help you. – BionicCode Dec 01 '21 at 22:57
  • @mm8 Hello, it seems like the issue was with ScrollViewer.CanContentScroll="True". When I set it to false and set each DataGridTextColumn width to *, the datagrid takes up all the available space. – Kafka Dec 02 '21 at 15:48
  • @mm8 And when I maximize the window the Datagrid columns increase in width to take up all the available space, which is what I wanted. But weirdly enough, then when I minimize the Window again, the datagrid does not resize and remains at its full width and therefore a horizontal scrollbar appears on the grid. Does anyone know why that is happening? – Kafka Dec 02 '21 at 15:52

0 Answers0