WPF ScrollViewer 与网格渲染不正确

WPF ScrollViewer with Grid rendering incorrectly

使用这个简单的代码:

<Window x:Class="WpfApplication1.Window2"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Window2" Height="340" Width="600">
    <ScrollViewer>
        <Grid Background="Red" VerticalAlignment="Top">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="Auto" MinWidth="20" />
                <ColumnDefinition />
            </Grid.ColumnDefinitions>

            <Grid.RowDefinitions>
                <RowDefinition />
                <RowDefinition />
                <RowDefinition />
                <RowDefinition />
            </Grid.RowDefinitions>

            <TextBlock Grid.Row="0" Text="1:" />
            <TextBox Grid.Row="0" Grid.Column="1" Height="80" Margin="4" />

            <TextBlock Grid.Row="1" Text="2:" />
            <TextBox Grid.Row="1" Grid.Column="1" Height="80" Margin="4" />

            <TextBlock Grid.Row="2" Text="3:" />

            <TextBlock Grid.Row="3" Grid.Column="0" Text="4.:" />
        </Grid>
    </ScrollViewer>
</Window>

Visual Studio 设计师正确显示了所有内容:

然而,在运行期间,结果是这样的:

请注意第 4 行如何不再位于网格内?!它在它之外呈现。 另外,如果你缩短 window,垂直滚动条确实可见,但它只滚动红色区域。无法滚动查看第 4 行。

如果从 Grid 中删除 VerticalAlignment="Top",则渲染似乎已修复,但滚动仍然无法正常工作。

谁能解释一下到底是怎么回事?这是 Microsoft 的错误吗?

我是运行Visual Studio2017社区版(全面更新),Win 10版本1803,包含.NET 4.7.2。

(我认为是.NET 4.7.2相关的,因为我到现在才注意到这个问题)

有一个解决方法,为所有行指定 - <RowDefinition Height="Auto" />,但这不是必需的...

这是将 space 分配给 *-行的新算法中的一个错误。 (当应用以 4.7+ 为目标,或者当您安装了 4.7+ 并设置了 Switch.System.Windows.Controls.Grid.StarDefinitionsCanExceedAvailableSpace=false 时,应用会使用新算法。)

早前报告了该错误(请参阅 https://github.com/Microsoft/dotnet/issues/674), and is already fixed in 4.8 (see https://github.com/Microsoft/dotnet-framework-early-access/blob/master/release-notes/NET48/build-3632/dotnet-build-3632-changes.md#wpf)。