WPF 网格中的独立宽度 [英] Independent width in a WPF Grid

查看:32
本文介绍了WPF 网格中的独立宽度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 WPF 中有一个 2 行 2 列的网格.我希望每行的列宽都是独立的.我试过自动",但没有成功.下面是一张图来说明:

I have a grid with 2 rows and 2 columns in WPF. I would like that the column widths are independent for each row. I tried "auto", but no success. Here is a picture in order to explain:

如何使用网格来完成此操作?

How can I accomplish this using grid?

推荐答案

如果你必须使用网格布局,那么你有几个选择:

If you must use a grid layout, then you have a couple of options:

选项 1:将每一行设为单列,然后在您希望独立列的每一行中嵌套一个网格:

Option 1: Make each row a single column and then nest a grid in each row you would like independent columns:

XAML

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition />
        <RowDefinition />
    </Grid.RowDefinitions>

    <TextBlock Text="AAAAAAAAAAAAAAAAAAAA" />

    <Grid Grid.Row="1">
      <Grid.ColumnDefinitions>
        <ColumnDefinition />
        <ColumnDefinition />
      </Grid.ColumnDefinitions>

      <TextBlock Text="BBBBBBB"">
      <TextBlock Grid.Column="1" Text="CCCCCCC" />
    </Grid>
</Grid>

选项 2:在行中使用 ColumnSpan:

Option 2: Make use of ColumnSpan in the rows:

XAML

<Grid>
  <Grid.RowDefinitions>
    <RowDefinition />
    <RowDefinition />
  </Grid.RowDefinitions>

  <Grid.ColumnDefinitions>
    <ColumnDefinition />
    <ColumnDefinition />
  </Grid.ColumnDefinitions>

      <TextBlock Grid.ColumnSpan="2" Text="AAAAAAAAAAAAAAAAAAAA" />
      <TextBlock Grid.Row="1" Text="BBBBBBB"">
      <TextBlock Grid.Row="1" Grid.Column="1" Text="CCCCCCC" />
    </Grid>
</Grid>

*这些是在没有编辑器的情况下键入的,可能需要一些调整.

*These were typed without an editor and may need a bit of tweaking.

这篇关于WPF 网格中的独立宽度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆