在WPF中,如何使DataGrid适应窗口高度?

25

我有一个拥有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"

在数据表格中,滚动条没有作用,行会向下流动。某种程度上,数据表格感觉不受限制...

怎么办?


1
给数据网格一些宽度可能会解决你的问题...如果你不想硬编码宽度,你可以做某种元素绑定来实现这个目标... - Bathineni
我担心高度... - GilShalit
抱歉,我只是在谈论高度问题而已,那是打字错误。给数据网格一些高度可能会解决你的问题。如果你不想硬编码高度,你可以使用某种元素绑定来实现这个目的。 - Bathineni
3个回答

49

尝试将DataGrid的HorizontalAlignment=StretchVerticalScrollBarVisibility=Auto设置。

如果这样做不起作用,您可能还需要将Grid的Height绑定到窗口高度,以便它不会根据其内容自动增长。通常我使用Height="{Binding RelativeSource={RelativeSource AncestorType={x:Type Window}}, Path=ActualHeight}"(也许是RenderSize.ActualHeight而不仅仅是ActualHeight...我忘了)。

另一个选择是使用DockPanel而不是Grid,因为该控件不会根据其内容自动增长。相反,它会拉伸其最后一个子元素以填充剩余空间。


1
有趣的想法。我会尝试并回报结果 :-) - GilShalit
3
绑定技术非常有效,谢谢!正如你所写的那样,它是ActualPath。顺便问一下,我该如何将某物绑定到路径值的一部分? Path=ActualPath/2 似乎不起作用。 - GilShalit
3
你需要使用转换器来实现那个。 - Rachel

6
我曾遇到同样的问题,但是将高度绑定到窗口并不能完全解决我的问题。在我的情况下,数据表格仍然延伸到窗口可见区域以下2至3英寸。我认为这是因为我的数据表格从窗口顶部以下约2至3英寸开始。
最后我发现根本不需要绑定数据表格的高度。我所要做的就是改变数据表格的直接容器。
对于我来说,下面的XAML设置会导致在添加足够多的行时数据表格超出窗口的大小。请注意,数据表格位于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>

然而,对我来说,仅仅删除 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>

由于原帖相当古老,我应该指出我正在使用VS2017和.Net Framework 4.6.1,但我不确定这是否有任何影响。


我尝试在Visual Studio 2019 Enterprise中像上面那样在DataGrid中添加StackPanel,但是它给了我一个错误:“属性元素不能在元素内容的中间。它们必须在内容之前或之后。” - John Foll

0

你必须用 * 定义 DataGrid 的列和行。 你说它在左下角的单元格。行没问题,但你的列有 Width="Auto"

如果 Width="Auto"AutoGenerateColumns=True 会出现混乱。


请注意,即使您设置了“ScrollViewer.HorizontalScrollBarVisibility =”Auto“”,这也会导致水平滚动条不显示。 - mauris

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接