在 wpf 中,我如何使数据网格适合窗口高度
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6678194/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
in wpf how do i make a datagrid fit the window height
提问by GilShalit
I have a grid with 3 columns and 2 rows
我有一个 3 列 2 行的网格
<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.
在左下角的单元格中,我有一个数据网格,其中 AutoGenerateColumns=True 可以加载多行。我想要做的是使数据网格高度最大化以适应窗口,并使用户能够使用数据网格滚动条上下滚动行。
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?
该怎么办?
回答by Rachel
Try setting your DataGrid's HorizontalAlignment=Stretch
and VerticalScrollBarVisibility=Auto
尝试设置您的 DataGridHorizontalAlignment=Stretch
和VerticalScrollBarVisibility=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 be RenderSize.ActualHeight
instead of just ActualHeight
... I forgot.
如果这不起作用,您可能还需要将网格的高度绑定到窗口高度,以便它不会自动增长以适应其内容。通常我使用Height="{Binding RelativeSource={RelativeSource AncestorType={x:Type Window}}, Path=ActualHeight}"
(它可能RenderSize.ActualHeight
不仅仅是ActualHeight
......我忘记了。
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.
另一种选择是使用 aDockPanel
而不是 a ,Grid
因为该控件不会自动增长以适应其内容。相反,它会拉伸它的最后一个孩子来填充剩余的空间。
回答by Shimeon
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.
我遇到了同样的问题,但绑定到 Window 高度并没有完全解决我的问题。在我的例子中,DataGrid 仍然延伸到窗口可视区域下方 2 到 3 英寸处。我相信这是因为我的 DataGrid 从窗口顶部下方约 2 到 3 英寸处开始。
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.
最后我发现根本没有必要绑定DataGrid的高度。我所要做的就是更改 DataGrid 的直接容器。
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.
对我来说,当添加足够多的行时,以下 XAML 设置会导致 DataGrid 扩展到超出窗口的大小。请注意,DataGrid 位于 StackPanel 内。
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="75"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<StackPanel Grid.Row="0">
<!-- StackPanel Content accounting for about 2-3 inches of space -->
</StackPanel>
<!-- DataGrid within a StackPanel extends past the vertical space of the Window
and does not display vertical scroll bars. Even if I bind the height to Window
height the DataGrid content still extends 2-3 inches past the viewable Window area-->
<StackPanel Grid.Row="1">
<DataGrid ItemsSource="{StaticResource ImportedTransactionList}"
Margin="10,20,10,10" MinHeight="100">
</DataGrid>
</StackPanel>
</Grid>
However, simply removing the StackPanel fixed the problem for me.
但是,只需删除 StackPanel 即可为我解决问题。
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="75"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<StackPanel Grid.Row="0">
<!-- StackPanel Content accounting for about 2-3 inches of space -->
</StackPanel>
<!-- Removing the StackPanel fixes the issue-->
<DataGrid Grid.Row="1" ItemsSource="{StaticResource SomeStaticResource}"
Margin="10,20,10,10" MinHeight="100">
</DataGrid>
</Grid>
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.
由于原始帖子很旧,我应该注意我使用的是 VS2017 和 .Net Framework 4.6.1,但我不确定这是否有任何影响。
回答by Dummy01
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"
.
您必须定义 DataGrid 所在的列和行 *
。你说它在左下角的单元格上。该行没问题,但您的列有Width="Auto"
.
The AutoGenerateColumns=True
gives a mess if Width="Auto"
.
在AutoGenerateColumns=True
给出了一个烂摊子,如果Width="Auto"
。