在wpf中如何使数据网格适合窗口高度
我有一个包含 3 列和 2 行的网格,
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="10"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
位于左下单元格,我有一个数据网格,其中 AutoGenerateColumns=True 可以加载多行。我想要做的是使数据网格高度最大化以适合窗口,并且用户能够使用数据网格滚动条上下滚动行。
发生的情况是数据网格从窗口底部流动,即使我设置了
ScrollViewer.VerticalScrollBarVisibility="Visible"
数据网格,滚动条也没有效果,行向下流动。不知怎的,数据网格并没有感觉到受到限制......
该怎么办?
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>
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...
What to do?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
尝试设置 DataGrid 的
HorizontalAlignment=Stretch
和VerticalScrollBarVisibility=Auto
如果这不起作用,您可能还需要将网格的高度绑定到窗口高度,这样它就不会起作用t 自动增长以适应其内容。通常我使用
Height="{BindingrelativeSource={RelativeSource AncestorType={x:Type Window}}, Path=ActualHeight}"
(可能是RenderSize.ActualHeight
而不是只是ActualHeight
...我忘记了另一种选择是使用
DockPanel
而不是Grid
因为该控件没有。相反,它会拉伸最后一个子元素以填充剩余空间。Try setting your DataGrid's
HorizontalAlignment=Stretch
andVerticalScrollBarVisibility=Auto
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 beRenderSize.ActualHeight
instead of justActualHeight
... I forgot.Another alternative is to use a
DockPanel
instead of aGrid
since that control doesn't auto-grow to fit its contents. Instead it'll stretch its last child to fill the remaining space.我遇到了同样的问题,但绑定到窗口高度并没有完全解决我的问题。在我的例子中,DataGrid 仍然延伸到窗口可视区域下方 2 到 3 英寸。我相信这是因为我的 DataGrid 开始于窗口顶部下方约 2 到 3 英寸处。
最后我发现根本没有必要绑定DataGrid的高度。我所要做的就是更改 DataGrid 的直接容器。
对于我来说,以下 XAML 设置会导致在添加足够的行时 DataGrid 超出窗口的大小。请注意,DataGrid 位于 StackPanel 内。
然而,简单地删除 StackPanel 就解决了我的问题。
由于原始帖子相当旧,我应该注意到我正在使用 VS2017 和 .Net Framework 4.6.1,但我不确定这是否有任何影响。
I had the same problem but binding to the Window height did not completely solve the problem for me. In my case the DataGrid still extended 2 to 3 inches below the Window's viewable area. I believe this was because my DataGrid started about 2 to 3 inches below the top of the Window.
In the end I found that it was not necessary to bind the DataGrid's height at all. All I had to do was change the DataGrid's direct container.
For me, the following XAML setup causes the DataGrid to extend beyond the size of the Window when enough rows are added. Notice that the DataGrid sits within a StackPanel.
However, simply removing the StackPanel fixed the problem for me.
As the original post is quite old, I should note that I am using VS2017 and .Net Framework 4.6.1 but I am not sure if this has any bearing.
您必须使用
*
定义 DataGrid 所在的列和行。你说它在左下角的单元格上。该行没问题,但您的列有
Width="Auto"
。如果
Width="Auto"
,AutoGenerateColumns=True
会造成混乱。You have to define the column and row where the DataGrid is with
*
.You say it is on the lower left cell. The row is ok but your column there has
Width="Auto"
.The
AutoGenerateColumns=True
gives a mess ifWidth="Auto"
.