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...
对于datagrid,滚动条无效,行向下流动。不知何故,数据网格不会受到限制......
What to do?
该怎么办?
2 个解决方案
#1
32
Try setting your DataGrid's HorizontalAlignment=Stretch
and VerticalScrollBarVisibility=Auto
尝试设置DataGrid的HorizontalAlignment = 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.
另一种方法是使用DockPanel而不是Grid,因为该控件不会自动增长以适应其内容。相反,它会伸展它的最后一个孩子来填补剩余的空间。
#2
1
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所在的列和行*。你说它在左下角的单元格上。该行没问题但是你的列有宽度=“自动”。
The AutoGenerateColumns=True
gives a mess if Width="Auto"
.
如果Width =“Auto”,则AutoGenerateColumns = True会导致混乱。
#1
32
Try setting your DataGrid's HorizontalAlignment=Stretch
and VerticalScrollBarVisibility=Auto
尝试设置DataGrid的HorizontalAlignment = 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.
另一种方法是使用DockPanel而不是Grid,因为该控件不会自动增长以适应其内容。相反,它会伸展它的最后一个孩子来填补剩余的空间。
#2
1
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所在的列和行*。你说它在左下角的单元格上。该行没问题但是你的列有宽度=“自动”。
The AutoGenerateColumns=True
gives a mess if Width="Auto"
.
如果Width =“Auto”,则AutoGenerateColumns = True会导致混乱。