【问题标题】:WPF: How to combine Rows within a column (alternative to rowspan)?WPF:如何在列中组合行(替代行跨度)?
【发布时间】:2010-12-01 19:06:51
【问题描述】:

有没有办法在特定列中组合行?因此得到这样的东西(我目前在控件上使用行跨度,即图像,但有更好的方法吗?)

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

我基本上都在用这段代码

    <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 的图像,但是否不可能设计一个没有行的列而其他列有行?

【问题讨论】:

    标签: wpf gridview layout wpf-controls


    【解决方案1】:

    Grid 控件无法做到这一点。行穿过所有列,列穿过所有行。如您所见,RowSpanColumnSpan 允许您分别控制多个行或列。

    另一种可能的解决方法是将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>
    

    【讨论】:

    • 谢谢肯特,但我想额外的代码膨胀不值得吗? .. 我认为继续使用我的网格并只使用 RowSpan 等会更好?
    • @Mark:是的,从你的描述来看,我认为RowSpan 是实现目标的最简单方法。
    【解决方案2】:

    这样的事情怎么样:

                <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>
    

    【讨论】:

      【解决方案3】:

      尝试使用矩形来合并 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>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-07-11
        • 2020-09-01
        • 2011-09-06
        • 2020-07-23
        相关资源
        最近更新 更多