Grid in a Grid with RowDefinition Height="auto" 抑制子 Grid 的 RowDefinitions 的 Height="*"

Grid in a Grid with RowDefinition Height="auto" suppresses Height="*" of child Grid's RowDefinitions

我有以下代码:

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="*"/>
        </Grid.RowDefinitions>
    <Grid Grid.Row="0" TextBlock.Foreground="Blue" ShowGridLines="True">
        <Grid.RowDefinitions>
            <RowDefinition Height="*"/>
            <RowDefinition Height="*"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>
        <TextBlock Text="test" Grid.Row="0" />
        <TextBlock Text="test" Grid.Row="2" />
    </Grid>

    <Grid Grid.Row="1" TextBlock.Foreground="Red" ShowGridLines="True">
        <Grid.RowDefinitions>
            <RowDefinition Height="*"/>
            <RowDefinition Height="*"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>
        <TextBlock Text="test" Grid.Row="0"/>
        <TextBlock Text="test" Grid.Row="2"/>
    </Grid>
</Grid>

它只是两个相等的三行网格,所有三行的大小应该相等(也就是说,每个网格都在自己的网格中)。

包含在高度为“*”的父行中的底部网格按预期运行。无论放入什么内容,每一行的大小都相同。

但是包含在高度为 Auto 的行中的顶部网格似乎丢弃了 Height="*",并且表现得好像它们具有 Height="Auto"。第一行和第三行恰好获得了他们要求的高度,第二行(空行)的高度仅为 0。这是正常行为吗?如果是这样,为什么会这样?

它是这样显示的:

这就是我期望它的工作方式:

它表现正常。
当您设置 Height="*" 时,这意味着 填充 space、 的其余部分
Height="Auto" 意味着 适合所有人内部控件.
所以第一行适合你拥有的所有控件,它们只有两个,并且因为没有 Height 属性 设置为第一个内部grid 或 TextBlocks 它只需要等于 yourFirstTextBlock.Height + yourSecondTextBlock.Height.

的高度

此行为是预期的。 Height="*" 表示所有行将平均共享可用 space

The value is expressed as a weighted proportion of available space

当您将父行高设置为自动时,这意味着子 Grid 不再垂直拉伸,因此没有可用的 space 共享,因此行将只占用 [=24] =] 因为他们需要。就像您设置 VerticalAlignment="Top" 一样。

你可以通过在顶部Grid上使用SharedSizeGroup来实现你想要的。在这种情况下,所有行都属于同一组,并且所有行都将共享相同的高度

<Grid Grid.Row="0" IsSharedSizeScope="True" TextBlock.Foreground="Blue" ShowGridLines="True" >
   <Grid.RowDefinitions>
      <RowDefinition SharedSizeGroup="CommonRow"/>
      <RowDefinition SharedSizeGroup="CommonRow"/>
      <RowDefinition SharedSizeGroup="CommonRow"/>
   </Grid.RowDefinitions>
   <TextBlock Text="test" Grid.Row="0" />
   <TextBlock Text="test" Grid.Row="2" />
</Grid>