dotnet / dotnet/wpf

UseLayoutRounding with GridUnitType.Auto causing unexpected gaps on collapsed Grids

Open
#6,615 1 comment 0 reactions 1 assignee Claimed by @dipeshmsft View on GitHub
Investigate
Dominant language
C#
Stars
7.7k
Forks
1.3k
Avg merge
1d 11h
Merged PRs (30d)
61

Description

* .NET Core Version: Tested on 4.8 net5.0 and net6.0
* Windows version: Tested on windows 10
* Does the bug reproduce also in WPF for .NET Framework 4.8?: Yes

**Problem description:**

Setting UseLayoutRounding to true when using grids with GridUnitType.Auto results in a collapsed grid's height being approx 1px

**Actual behavior:**

Unexpected gaps between visible grids.

![grid](https://user-images.githubusercontent.com/13840390/169865924-b27ba8f6-c471-4701-8445-c9c79005a7f3.PNG)

**Expected behavior:**

No gaps to appear.

**Noticed**

On my development machine.
Without UseLayoutRounding the Grids actual heights are 15.xxx but when using LayoutRounding it's rounded off to 17,
When I would have expected 16.

Adding an extra unused RowDefinition with GridUnitType.Star seems to fix the problem.
The heights are 16 as I would expect.
There are no unexpected gaps.

While this is a potential workaround for us, it's not ideal.

**Minimal repro:**

Default WPF application.

Name root grid element to layoutroot

Using code behind for simplicity vs XAML directly (Issue seen in both)

this.UseLayoutRounding = true;

for (int i = 0; i < 15; i++)
{
var rowdefinition = new RowDefinition()
{
Height = new GridLength(1, GridUnitType.Auto)
};

this.layoutroot.RowDefinitions.Add(rowdefinition);

var grid = new Grid();

var textBlock = new TextBlock()
{
Text = "Test"
};

grid.Children.Add(textBlock);
grid.Background = new SolidColorBrush(Colors.Red);

Grid.SetRow(grid, i);

if (i % 3 == 0)
{
grid.Visibility = Visibility.Collapsed;
}

this.layoutroot.Children.Add(grid);
}

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.