WPF-ItemsControl中的UniformGrid显示表标题

时间:2018-07-11 08:09:23

标签: wpf

我想显示一个表格,该表格大于我们的容器时,它会显示在旁边,例如:

1 A B C   4 A B C
2 A B C   5 A B C
3 A B C   6 A B C

我使用了ItemsControl和UniformGrid,并且想要添加表头,如下所示:

id title1 title2 title3   id title1 title2 title3
1   A      B      C       4   A      B      C
2   A      B      C       5   A      B      C
3   A      B      C       6   A      B      C

这是我的代码:

        <HeaderedItemsControl ItemsSource="{Binding Data}">
            <HeaderedItemsControl.Template>
                <ControlTemplate TargetType="HeaderedItemsControl">
                    <Border Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Padding="{TemplateBinding Padding}" SnapsToDevicePixels="true">
                        <DockPanel>
                            <ContentPresenter DockPanel.Dock="Top" ContentSource="Header"/>
                            <ItemsPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
                        </DockPanel>
                    </Border>
                </ControlTemplate>
            </HeaderedItemsControl.Template>
            <HeaderedItemsControl.HeaderTemplate>
                <DataTemplate>
                    <Grid Background="Red" HorizontalAlignment="Stretch">
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="1*"/>
                            <ColumnDefinition Width="4*"/>
                            <ColumnDefinition Width="1*"/>
                            <ColumnDefinition Width="1*"/>
                        </Grid.ColumnDefinitions>
                        <TextBlock Grid.Column="0" Text="Kód"/>
                        <TextBlock Grid.Column="1" Text="Popis"/>
                        <TextBlock Grid.Column="2" Text="Zmetky"/>
                        <TextBlock Grid.Column="3" Text="Oprava"/>
                    </Grid>
                </DataTemplate>
            </HeaderedItemsControl.HeaderTemplate>
            <ItemsControl.ItemsPanel>
                <ItemsPanelTemplate>
                    <UniformGrid Columns="2" Rows="15"/>
                </ItemsPanelTemplate>
            </ItemsControl.ItemsPanel>
            <ItemsControl.ItemTemplate>
                <DataTemplate>
                    <Border BorderBrush="#D3D3D3" Margin="3 3 30 3" BorderThickness="0 0 0 1">
                        <Grid x:Name="table">
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="1*"/>
                                <ColumnDefinition Width="4*"/>
                                <ColumnDefinition Width="1*"/>
                                <ColumnDefinition Width="1*"/>
                            </Grid.ColumnDefinitions>

                            <TextBlock Grid.Column="0" Text="{Binding Code}"/>
                            <TextBlock Grid.Column="1" Text="{Binding Description}"/>
                            <TextBox Grid.Column="2" Text="{Binding Number1}"/>
                            <TextBox Grid.Column="3" Text="{Binding Number2}"/>
                        </Grid>
                    </Border>
                </DataTemplate>
            </ItemsControl.ItemTemplate>
        </HeaderedItemsControl>

现在看起来像这样:

id             title1        title2         title3  
    1   A      B      C       4   A      B      C
    2   A      B      C       5   A      B      C
    3   A      B      C       6   A      B      C

如何添加带有标题的行?谢谢

0 个答案:

没有答案