【WPF】Grid的用法

  1. 只使用行
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition/>
            <RowDefinition/>
        </Grid.RowDefinitions>
        <TextBlock Text="AAAAA" Grid.Row="0"/>
        <TextBlock Text="BBBBB" Grid.Row="1"/>
    </Grid>
  1. 只使用列
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition/>
            <ColumnDefinition/>
        </Grid.ColumnDefinitions>
        <TextBlock Text="AAAAA" Grid.Column="0"/>
        <TextBlock Text="BBBBB" Grid.Column="1"/>
    </Grid>
  1. 行列组合
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition/>
            <RowDefinition/>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition/>
            <ColumnDefinition/>
        </Grid.ColumnDefinitions>
        <TextBlock Text="AAAAA" Grid.Row="0" Grid.Column="0"/>
        <TextBlock Text="BBBBB" Grid.Row="0" Grid.Column="1"/>
        <TextBlock Text="CCCCC" Grid.Row="1" Grid.Column="0"/>
        <TextBlock Text="DDDDD" Grid.Row="1" Grid.Column="1"/>
    </Grid>
  1. 行列组合,且允许合并
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition/>
            <RowDefinition/>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="Auto"/>
            <ColumnDefinition/>
        </Grid.ColumnDefinitions>
        <TextBlock Text="AAAAA" Grid.Row="0" Grid.Column="0"/>
        <TextBlock Text="BBBBB" Grid.Row="0" Grid.Column="1"/>
        <TextBlock Text="CCCCCCCCCC" Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="2"/>
        <TextBlock Text="DDDDD" Grid.Row="1" Grid.Column="1"/>
    </Grid>

此时5个C会和五个D重叠。

除了ColumnSpan,还有RowSpan。

posted @ 2024-03-16 16:19  greencode  阅读(35)  评论(0编辑  收藏  举报