How to make a wpf datagrid fill all available space and resize with window? Mind that the datagrid is inside a grid.
5 Answers
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>
-
3You might also want to set
SizeToContent="WidthAndHeight"
on yourWindow
control. Oct 4, 2016 at 14:36 -
1
HorizontalAlignment="Stretch" VerticalAlignment="Stretch"
declarations are useless sinceStretch
is the default value.– OraceMar 16, 2022 at 9:56
- Never set height or width to it or its parent control
- 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 :)
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
-
-
1@MohamedKamelBouzekria, they why does it do exactly what I want it to do? Mar 26, 2019 at 13:21
-
@MohamedKamelBouzekria, thanks for the edit, it makes sense now. Mar 26, 2019 at 16:45
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>
-
3
-
Insert DataGrid directly to the Grid without wrapping with a panel.
<Grid>
<DataGrid>
</DataGrid>
</Grid>
-
This won't work as the DataGrid will take as much space as it needs and not fill all the space available Mar 25, 2019 at 17:44
-
1
code
.Grid
for this particular reason. Whenever I need my controls to be dynamically re-sized I useDockPanel
andStackPanel
to arrange my controls. It provides a lot of flexibility sinceDockPanel
fills the available space with the last defined logical child