简体   繁体   English

WPF网格中的独立宽度

[英]Independent width in a WPF Grid

I have a grid with 2 rows and 2 columns in WPF. 我在WPF中有2行2列的网格。 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: 如果必须使用网格布局,则有两种选择:

Option 1: Make each row a single column and then nest a grid in each row you would like independent columns: 选项1:将每一行设为一列,然后在您希望独立列的每一行中嵌套一个网格:

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

Option 2: Make use of ColumnSpan in the rows: 选项2:在各行中使用ColumnSpan:

XAML 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. *这些内容是在没有编辑器的情况下输入的,可能需要进行一些调整。

Two grids? 两个网格?

<StackPanel Width="277">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="Auto"/>
            <ColumnDefinition Width="*"/>
        </Grid.ColumnDefinitions>
        <TextBlock Text="AAAAAAAAAAAAAAAAAAAA" Grid.Row="0" Grid.Column="0"/>            
    </Grid>
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="Auto"/>
            <ColumnDefinition Width="*"/>
        </Grid.ColumnDefinitions>
        <TextBlock Text="BBBBBBB" Grid.Row="0" Grid.Column="0"/>
        <TextBlock Text="CCCCCCC" Grid.Row="0" Grid.Column="1"/>
    </Grid>
</StackPanel>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM