.netwpfscrollviewerwpf-grid

WPF ScrollViewer with Grid rendering incorrectly


Take this simplistic code:

<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 designer shows everything correctly: enter image description here

And yet, during runtime, this is the result: enter image description here

Notice how row nr 4 is no longer inside the Grid?! It is rendered outside of it. Also, if you shorten the window, the vertical scroll bar does become visible, but it only scrolls the red area. It is not possible to scroll to see row 4.

If you remove VerticalAlignment="Top" from the Grid, then the rendering seems to be fixed, but scrolling still does not work properly.

Can anyone explain what on earth is going on? Is it a Microsoft bug?

I'm running Visual Studio 2017 Community edition (fully updated), Win 10 version 1803, which contains .NET 4.7.2.

(I think it is related to .NET 4.7.2, because I've never noticed this problem until now)

There is a workaround, specify - <RowDefinition Height="Auto" /> for all rows, but that should not be necessary...


Solution

  • This is a bug in the new algorithm for allocating space to *-rows. (An app uses the new algorithm when it targets 4.7+, or when you have installed 4.7+ and have set Switch.System.Windows.Controls.Grid.StarDefinitionsCanExceedAvailableSpace=false.)

    The bug was reported earlier (see 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).