Wpf 自动调整网格行的大小,当具有RowSpan的项跨行时,会执行奇怪的大小调整

Wpf 自动调整网格行的大小,当具有RowSpan的项跨行时,会执行奇怪的大小调整,wpf,Wpf,所以我基本上有以下代码: <Grid> <Grid.RowDefinitions> <RowDefinition Height="Auto"/> <RowDefinition Height="Auto"/> <RowDefinition Height="Auto"/> <RowDefinition Height="*"/> </Grid.R

所以我基本上有以下代码:

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="*"/>
    </Grid.RowDefinitions>

    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="*"/>
        <ColumnDefinition Width="*"/>
    </Grid.ColumnDefinitions

    <TextBlock Grid.Row="0" Grid.Column="0" Text="Row0"/>
    <TextBlock Grid.Row="1" Grid.Column="0" Text="Row1"/>

    <GroupBox Grid.Row="0" Grid.RowSpan="3" Grid.Column="1">
        <!-- Some stuff here in the GroupBox -->
    </GroupBox>

    <!-- Some other stuff in Grid.Row="3" -->
</Grid>


发生的情况是,前两行的大小与其中的文本块不匹配,它们的大小受到GroupBox的影响。我添加了第三行基本上只是为了提供溢出空间,因为GroupBox比前两行加在一起稍微高一点,但这也使得前两行的大小增加了,类似于如果它们是星形大小的话我所期望的。如果我注释掉GroupBox,前两行的大小将正确地对应于它们各自的文本块。关于发生这种情况的原因以及如何使其达到我认为应该达到的效果,您有什么想法吗?

请为您的行尝试以下设置:

<Grid.RowDefinitions>
  <RowDefinition Height="Auto"/>
  <RowDefinition Height="Auto"/>
  <RowDefinition Height="0"/>
  <RowDefinition Height="*"/>
</Grid.RowDefinitions>

以下是我最后做的事情,因为我无法在一个网格中工作:

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="*"/>
    </Grid.RowDefinitions>

    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="*"/>
        <ColumnDefinition Width="*"/>
    </Grid.ColumnDefinitions>

    <Grid Grid.Row="0" Grid.ColumnSpan="2">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
        </Grid.RowDefinitions>

        <TextBlock Grid.Row="0" Text="Row0"/>
        <TextBlock Grid.Row="1" Text="Row1"/>
    </Grid>

    <GroupBox Grid.Column="1" Grid.Row="0">
        <!-- Stuff in the GroupBox -->
    </GroupBox>

    <!-- Stuff in Grid.Row="1" -->
</Grid>


这不会让GroupBox溢出到第四行吗?我仍然希望第四行完全位于GroupBox下方。我不这么认为。不。我把一个快速的示例放在一起,上面的标记似乎达到了您想要的布局。试试看,让我知道。嗯,你说得对,但我不明白为什么这样行。如果行的高度为0,那么它的高度不应该始终为0,而不是扩展以适应la Auto的内容吗?老实说,我也不明白为什么它可以工作,我只是在试验。如果我有机会,我会仔细研究一下,看看这种行为是否有文档记录。我拿走了你接受的答案,因为它会带来一些意想不到的后果。乍一看,它似乎达到了我想要的效果,但问题是,星形大小的行不只是从顶部挤压一点,整个东西保持相同的大小,但实际上会随着控件溢出到零大小行的量而下移。因此,如果控件将50个像素向下溢出到零大小的行中,则星形大小行的底部50个像素根本不可见。只是不想让任何人在将来看到这一点时感到困惑。是的,我不认为它可以用一个单一的网格来完成。如果愿意,您可以将内部网格切换为
堆栈面板
,然后在添加其他行时,无需更新
行定义