在 wpf 中,我如何使数据网格适合窗口高度 [英] in wpf how do i make a datagrid fit the window height
问题描述
我有一个 3 列 2 行的网格
I have a grid with 3 columns and 2 rows
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="10"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
在左下方的单元格中,我有一个数据网格,其中 AutoGenerateColumns=True 可以加载多行.我想要做的是让数据网格高度最大化以适应窗口,并且让用户能够使用数据网格滚动条上下滚动行.
I the lower left cell, i have a data grid, with AutoGenerateColumns=True which can load many rows. What I want to do is for the data grid height to maximize to fit the window, and for the user to be able to use the datagrid scrollbar to scroll the rows up and down.
发生的情况是窗口底部的数据网格流动,即使我设置了
What happens is that the datagrid flows of the bottom of the window, and even if i set the
ScrollViewer.VerticalScrollBarVisibility="Visible"
的数据网格,滚动条不起作用,行向下流动.不知何故,数据网格不会受到限制......
of the datagrid, the scrollbar has no effect and the rows flow downwards. Somehow the datagrid does not feel restricted...
怎么办?
推荐答案
尝试设置 DataGrid 的 HorizontalAlignment=Stretch
和 VerticalScrollBarVisibility=Auto
Try setting your DataGrid's HorizontalAlignment=Stretch
and VerticalScrollBarVisibility=Auto
如果这不起作用,您可能还需要将网格的高度绑定到窗口高度,以便它不会自动增长以适应其内容.通常我使用 Height="{Binding RelativeSource={RelativeSource AncestorType={x:Type Window}}, Path=ActualHeight}"
(它可能是 RenderSize.ActualHeight
而不是只是ActualHeight
...我忘了.
If that doesn't work, you may also need to bind the Grid's Height to the Window Height so that it doesn't auto-grow to fit its contents. Usually I use Height="{Binding RelativeSource={RelativeSource AncestorType={x:Type Window}}, Path=ActualHeight}"
(It might be RenderSize.ActualHeight
instead of just ActualHeight
... I forgot.
另一种替代方法是使用 DockPanel
而不是 Grid
,因为该控件不会自动增长以适应其内容.相反,它会拉伸它的最后一个孩子来填充剩余的空间.
Another alternative is to use a DockPanel
instead of a Grid
since that control doesn't auto-grow to fit its contents. Instead it'll stretch its last child to fill the remaining space.
这篇关于在 wpf 中,我如何使数据网格适合窗口高度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!