我正在看这个问题并发现了一些非常奇怪的东西:在某些情况下,似乎错误地计算了一行的高度Grid.RowSpan
.
这是Grid
我正在测试的简单图纸:
--------------- | 1 | | --------| 3 | | 2 | | --------------- | 4 | ---------------
以下是此网格的一些示例代码,用于演示此问题:
<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>
Run Code Online (Sandbox Code Playgroud)
最终结果是第3行的高度(单元格#2和#3)中有很多额外的空间:
如果我Grid.RowSpan
将第1和第3个单元格调整Grid.Row
为+/- 1,并将第2个和第4 个单元格调整为+/- 1以考虑额外的行,我得到这个(正确的)结果:
如果我从单元格#3中删除了足够多的元素,那么我也会得到正确的结果,因此它可以在单行中渲染,如下所示:
奇怪的是,删除一些对象会导致只应用一些额外的空间
我一直在弄乱单元格#1和#3中的元素数量以及行数,但我似乎无法找出解释这种行为的结论性模式.
当渲染这个Grid时,WPF在幕后做什么才能Grid.RowSpan
在单元格#3上出现额外的空间?
我没有关于为什么 .NET 的第三行出错的完整答案。
但我认为你要求它做的事情是不合逻辑的,因为没有理由让 0,0 跨越两行。
当共享行时,它们的长度可能不相等,WPF 必须将长度添加到较短的行中。
在您的情况下,由于共享行中有共享行,因此 WPF 必须应用一些权重,但不能正确执行此操作。
如果你不跨越 0 0 那么它与第 0 行第 0 列和第 1 行第 0 列平等地共享额外的空间,这(对我来说)是正确的答案。
<Window x:Class="GridRowSizing.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<Style BasedOn="{StaticResource {x:Type Label}}" TargetType="Label">
<Setter Property="BorderBrush" Value="Black"/>
<Setter Property="BorderThickness" Value="1"/>
<Setter Property="Margin" Value="3"/>
</Style>
</Window.Resources>
<Grid ShowGridLines="True">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<StackPanel Grid.Column="0" Grid.Row="0" Background="Red">
<Label Content="CELL 1 A"/>
<Label Content="CELL 1 B"/>
<Label Content="CELL 1 C" BorderBrush="Black" BorderThickness="2"/>
</StackPanel>
<StackPanel Grid.Column="0" Grid.Row="1" Background="CornflowerBlue">
<Label Content="CELL 2 D" BorderBrush="Black" BorderThickness="2"/>
<Label Content="CELL 2 E" BorderBrush="Black" BorderThickness="2"/>
</StackPanel>
<StackPanel Grid.Column="1" Grid.Row="0" Grid.RowSpan="2" Background="Yellow">
<Label Content="CELL 3 A"/>
<Label Content="CELL 3 B"/>
<Label Content="CELL 3 C"/>
<Label Content="CELL 3 D" BorderBrush="Black" BorderThickness="2"/>
<Label Content="CELL 3 E" BorderBrush="Black" BorderThickness="2"/>
<Label Content="CELL 3 F" BorderBrush="Black" BorderThickness="2"/>
</StackPanel>
<StackPanel Grid.Column="0" Grid.Row="2" Grid.ColumnSpan="2" Background="Green">
<Label Content="CELL 4"/>
</StackPanel>
</Grid>
</Window>
Run Code Online (Sandbox Code Playgroud)