【WPF】Grid的用法

greencode發表於2024-03-16
  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。

相關文章