Search code examples
wpflayoutdatagridtabcontroltabitem

How do I get a DataGrid to fill a TabItem


I have the following layout

<DockPanel>
    <StackPanel DockPanel.Dock="Top" Orientation="Horizontal" Margin="10">
        ...
    </StackPanel>
    <TabControl>
        <TabItem Header="Summary">
            <DataGrid   ItemsSource="{Binding SummaryData}"
                        CanUserAddRows="False"
                        CanUserDeleteRows="False"
                        IsReadOnly="True"
                        HeadersVisibility="Column"
                        CanUserSortColumns="False" />
        </TabItem>
        ...
    </TabControl>
</DockPanel>

Without the DataGrid, the TabControl and TabItems fill the rest of the container perfectly, but when I add the DataGrid it stretches everything out to display all of the rows and columns.

EDIT: more clarity

I am looking to have the DataGrid stretch veritcally and horizontally to fill the TabItem. If it needs more space, I'd like to have the DataGrid's scrollbars appear.


Solution

  • I got the following to do what I want.

    <TabItem Header="Summary" >
        <Grid x:Name="SummaryGrid">
            <DataGrid Height="{Binding ElementName=SummaryGrid, Path=ActualHeight}"
                      ItemsSource="{Binding SummaryData}"
                      CanUserAddRows="False"
                      CanUserDeleteRows="False"
                      IsReadOnly="True"
                      HeadersVisibility="Column"
                      CanUserSortColumns="False" />
        </Grid>
    </TabItem>
    

    It works until I change the size of the parent panel. The ActualHeight isn't updated. It is good enough for now though.