Independent width in a WPF Grid

后端 未结 2 1665
一向
一向 2021-01-26 00:32

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

相关标签:
2条回答
  • 2021-01-26 00:44

    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:

    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:

    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.

    0 讨论(0)
  • 2021-01-26 00:54

    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>
    
    0 讨论(0)
提交回复
热议问题