Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Datagrid extends off window

Tags:

wpf

xaml

datagrid

Hey everyone. I have a problem with a datagrid in my window. I want it to extend only to its needed size if it doesn't fill the entire window or show a scroll bar if it does take up the entire screen. Here's my xaml

 <Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="*"/>
        <RowDefinition Height="auto"/>
        <RowDefinition Height="auto"/>
    </Grid.RowDefinitions>

    <DataGrid Grid.Row="0" Name="dg">
        <DataGrid.Columns>
            <DataGridTextColumn Header="Col1"/>
            <DataGridTextColumn Header="Col1"/>
        </DataGrid.Columns>            
    </DataGrid>

    <GroupBox Grid.Row="2" Margin="5">
        <Button>Click</Button>
    </GroupBox>
</Grid>

If I set the row height for the datagrid to * it extends the datagrid's gray background down the entire row. But if I set the height to auto, then it won't show a scrollbar when there are too many items for the window.

Any ideas?

like image 502
wangburger Avatar asked May 03 '26 21:05

wangburger


1 Answers

Have you tried nesting the DataGrid in a ScrollViewer?

like image 89
cunningdave Avatar answered May 05 '26 11:05

cunningdave



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!