简体   繁体   中英

How to make a wpf datagrid fill all available space and resize with window?

How to make a wpf datagrid fill all available space and resize with window? Mind that the datagrid is inside a grid.

You should be able to do it for a datagrid (or for any WPF control) by setting HorizontalAlignment and VerticalAlignment to Stretch

If it's inside a Grid, you should be able to set something like this

<Grid> <!-- parent grid or whatever -->
   <DataGrid HorizontalAlignment="Stretch" VerticalAlignment="Stretch" ... >
   </DataGrid>
</Grid>
  1. Never set height or width to it or its parent control
  2. No need to worry on the horizontal and vertical alignment since by default Horizontal and vertical alignment are stretch. if above are proper things should work :)

Remove all height and width property of its parent control. remove horizontal and vertical property. Define row height as * of its parent grid.

<Grid> 
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
   <DataGrid Grid.Row="0" >
   </DataGrid>
</Grid>

Set the width for the last column of the DataGrid to *.

<DataGridTextColumn Header="Notes"
                    Binding="{Binding Notes}"
                    Width="*"/>

Note : This actually works on only the last column of the DataGrid

Insert DataGrid directly to the Grid without wrapping with a panel.

    <Grid>
       <DataGrid>
       </DataGrid>
    </Grid>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM