WPF:如何在列中合并行(替代rowspan)?

10

有没有一种方法可以将特定列中的行合并? 因此,要获得像这样的结果(我目前正在使用控件上的rowspan,即图片,但是否有更好的方法?)

    --------------------
    |         |--------|
    |         |--------|
    |         |--------|
    |         |--------|
    |         |--------|
    --------------------

我基本上使用这段代码

    <Grid.RowDefinitions>
        <RowDefinition Height="*" />
        <RowDefinition Height="28" />
        <RowDefinition Height="28" />
        <RowDefinition Height="28" />
        <RowDefinition Height="*" />
        <RowDefinition Height="28" />
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="124" />
        <ColumnDefinition Width="246*" />
    </Grid.ColumnDefinitions>

这样就会得到类似于这样的结果(注意行也会出现在第0列)

    --------------------
    |---------|--------|
    |---------|--------|
    |---------|--------|
    |---------|--------|
    |---------|--------|
    --------------------

现在我可以绕过这个问题,例如如果我想放置一张图片,我可以使用 RowSpan,但是有没有可能设计一个没有行的列,而其他列有行呢?

3个回答

11

使用 Grid 控件是不可能的。行会通过所有列,而列会通过所有行。正如您所发现的,RowSpanColumnSpan 允许您的控件跨越多个行或列。

另一个潜在的解决方法是在另一个 Grid 中托管一个 Grid

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

    <Image/>

    <Grid Grid.Column="1">
        <Grid.RowDefinitions>
            <RowDefinition Height="*"/>
            <RowDefinition Height="*"/>
            <RowDefinition Height="*"/>
            <RowDefinition Height="*"/>
            <RowDefinition Height="*"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>
    </Grid>
</Grid>

谢谢Kent,但我认为额外的代码膨胀不值得吧?我想继续使用我的网格并使用RowSpan等功能会更好吧? - mark smith
@Mark:是的,根据你的描述,我认为RowSpan是实现你目标最简单的方法。 - Kent Boogaart

1

这样怎么样:

            <StackPanel Orientation="Horizontal">
                <Grid Height="100" Width="50"></Grid>
                <Grid>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="*" />
                        <RowDefinition Height="*" />
                        <RowDefinition Height="*" />
                        <RowDefinition Height="*" />
                        <RowDefinition Height="*" />
                    </Grid.RowDefinitions>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="150" />
                    </Grid.ColumnDefinitions>
                </Grid>
            </StackPanel>

0
尝试使用矩形来合并这6行。
<Grid>
<Grid.RowDefinitions>
    <RowDefinition Height="*" />
    <RowDefinition Height="28" />
    <RowDefinition Height="28" />
    <RowDefinition Height="28" />
    <RowDefinition Height="*" />
    <RowDefinition Height="28" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
    <ColumnDefinition Width="124" />
    <ColumnDefinition Width="246*" />
</Grid.ColumnDefinitions>
<Rectangle Grid.RowSpan="6"/>
</Grid>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接