Kafka
Kafka

Reputation: 21

WPF Datagrid not taking up all available remaining space

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?

Upvotes: 0

Views: 359

Answers (0)

Related Questions