如何使用WPF设计复杂表

时间:2019-07-03 21:16:56

标签: c# wpf xaml

是否有可能具有2列的网格,并且每个网格具有不同的行数,例如第一列4列,第二列3列。

Table Example

我想要图片上的东西

1 个答案:

答案 0 :(得分:7)

您不能只用一个Grid来做到这一点,而是使用多个Grid控件,ColumnSpanRowSpan属性以及某种控件,可以生成这样的“网格”。

<Window x:Class="SOWPF.MainWindow"
        ...
        Title="MainWindow" Height="450" Width="800">
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition/>
            <ColumnDefinition/>
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition/>
            <RowDefinition/>
            <RowDefinition/>
            <RowDefinition/>
            <RowDefinition/>
        </Grid.RowDefinitions>

        <Border Grid.Column="0" Grid.Row="0" Grid.ColumnSpan="2" Margin="4" Background="Orange"/>

        <Border Grid.Column="0" Grid.Row="1" Margin="4" Background="Green"/>
        <Border Grid.Column="0" Grid.Row="2" Margin="4" Background="Green"/>
        <Border Grid.Column="0" Grid.Row="3" Margin="4" Background="Green"/>
        <Border Grid.Column="0" Grid.Row="4" Margin="4" Background="Green"/>

        <Border Grid.Column="1" Grid.Row="1" Grid.RowSpan="4" Margin="4" Background="Orchid">
            <Grid>
                <Grid.RowDefinitions>
                    <RowDefinition/>
                    <RowDefinition/>
                    <RowDefinition/>
                </Grid.RowDefinitions>

                <Border Grid.Row="0" Margin="4" Background="DeepSkyBlue"/>
                <Border Grid.Row="1" Margin="4" Background="DeepSkyBlue"/>
                <Border Grid.Row="2" Margin="4" Background="DeepSkyBlue"/>
            </Grid>
        </Border>
    </Grid>
</Window>

视图:

enter image description here