【问题标题】:Grid in a Grid with RowDefinition Height="auto" suppresses Height="*" of child Grid's RowDefinitionsRowDefinition Height="auto" 的 Grid 中的 Grid 抑制了子 Grid 的 RowDefinitions 的 Height="*"
【发布时间】:2015-01-30 13:10:03
【问题描述】:

我有以下代码:

<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。这是正常行为吗?如果是这样,为什么会这样?

它是这样显示的:

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

【问题讨论】:

  • 顶部网格没有丢弃* Width 标记,它实际上使用了它!为什么您可以将其视为Auto 是因为PARENT 网格只允许Auto。因此,您的内部网格正在使用 * 父级自动高度的宽度

标签: wpf


【解决方案1】:

这种行为是预期的。 Height="*" 表示所有行将共享均匀的可用空间

该值表示为可用空间的加权比例

当您将父行高度设置为自动时,这意味着子 Grid 不再垂直拉伸,因此没有可共享的可用空间,因此行将只占用它们需要的空间。例如,就像您将设置 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>

【讨论】:

  • 啊,所以它的实现方式与我预期的不同。这是否记录在某处?
  • 不是这些确切的词,但它与我对Grid 的期望一致,它没有垂直拉伸。它就是这样工作的。
【解决方案2】:

表现正常。
当您设置Height="*" 时,表示填充其余空间,
Height="Auto" 表示适合所有内部控件
所以第一行是适合您拥有的所有控件,它们只有两个,并且由于没有将 Height 属性设置为第一个内部网格或 TextBlocks,因此它只需要等于 yourFirstTextBlock.Height + yourSecondTextBlock.Height 的高度。

【讨论】:

    猜你喜欢
    • 2021-12-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-01
    • 1970-01-01
    • 2011-05-29
    • 1970-01-01
    • 2012-01-15
    相关资源
    最近更新 更多