我曾遇到同样的问题,但是将高度绑定到窗口并不能完全解决我的问题。在我的情况下,数据表格仍然延伸到窗口可见区域以下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,但我不确定这是否有任何影响。