I was looking at this question and discovered something very weird: it appears that the height of a row is incorrectly calculated in some cases involving Grid.RowSpan
.
Here's an simple drawing of the Grid
I'm testing with:
--------------- | 1 | | --------| 3 | | 2 | | --------------- | 4 | ---------------
And here's some sample code for this Grid that demonstrates the problem:
<Grid ShowGridLines="True">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<StackPanel Grid.Column="0" Grid.Row="0" Grid.RowSpan="2" Background="Red">
<Label Content="CELL 1 A"/>
<Label Content="CELL 1 B"/>
<Label Content="CELL 1 C"/>
</StackPanel>
<Grid Grid.Column="0" Grid.Row="2" Background="CornflowerBlue">
<Label Content="CELL 2 D"/>
</Grid>
<StackPanel Grid.Column="1" Grid.Row="0" Grid.RowSpan="3" Background="Yellow">
<Label Content="CELL 3 A"/>
<Label Content="CELL 3 B"/>
<Label Content="CELL 3 C"/>
<Label Content="CELL 3 D"/>
</StackPanel>
<Grid Grid.Column="0" Grid.Row="3" Grid.ColumnSpan="2" Background="Green">
<Label Content="CELL 4"/>
</Grid>
</Grid>
The end result is the height of the 3rd row (cell #2 and #3) has a lot of extra space in it:
If I adjust the Grid.RowSpan
of the 1st and 3rd cells by +/- 1, and adjust the Grid.Row
for the 2nd and 4th by +/- 1 to account for the extra row, I get this (correct) result:
I also get correct results if I remove enough elements from cell #3 so it can render in a single Row, like this:
And strangely enough, removing some the objects results in only some of the extra space being applied
I've been messing around with the number of elements in cells #1 and #3, and number of Rows, but I can't seem to figure out a conclusive pattern to explain this behavior.
What exactly is WPF doing behind the scenes when rendering this Grid to cause the extra space to appear when the Grid.RowSpan
on cell #3?