标题上方的WPF datagrid标题

时间:2018-07-20 10:24:06

标签: c# wpf datagridview datagrid wpfdatagrid

因此,我正在尝试使此数据网格更有条理:

datagrid

当前,我正在绑定Json文件中的数据,该文件在C#代码中令人发指。现在,我不想继续写“飞行员x”,“飞行员y”等……而在这里重复一遍。所以我想知道是否有可能在当前标题上方“添加”标题。假设我们在一个标题下有“ voornaam”,“ achternaam”,“ club”和“ licentie”,在标题下还有下一个“ voornaam”,“ achternaam”,“ club”,“ licentie”,然后是“ merk” ,“类型”和“ nummerplaat”。

我的意思是“标题”;

Header example

您可以在下面找到XAML页面的代码;

<Grid>
        <DataGrid x:Name="list_participants" ColumnHeaderStyle="{StaticResource HeaderStyle}" ColumnWidth="Auto" RowBackground="Transparent" Foreground="#fafafa" AlternatingRowBackground="#555a62" Background="Transparent" BorderThickness="0" AutoGenerateColumns="False">
            <DataGrid.Columns>
                <DataGridTextColumn Header="Team nummer" Binding="{Binding team_number}" />
                <DataGridTextColumn Header="Voornaam" Binding="{Binding team_data.pilot.firstname}" />
                <DataGridTextColumn Header="Achternaam" Binding="{Binding team_data.pilot.lastname}" />
                <DataGridTextColumn Header="Club" Binding="{Binding team_data.pilot.club}" />
                <DataGridTextColumn Header="Licentie" Binding="{Binding team_data.pilot.license}" />
                <DataGridTextColumn Header="Voornaam" Binding="{Binding team_data.navigator.firstname}" />
                <DataGridTextColumn Header="Achternaam" Binding="{Binding team_data.navigator.lastname}" />
                <DataGridTextColumn Header="Club" Binding="{Binding team_data.navigator.club}" />
                <DataGridTextColumn Header="Licentie" Binding="{Binding team_data.navigator.license}" />
                <DataGridTextColumn Header="Merk" Binding="{Binding car_data.brand}" />
                <DataGridTextColumn Header="Type" Binding="{Binding car_data.type}" />
                <DataGridTextColumn Header="Nummerplaat" Binding="{Binding car_data.license_plate}" />
                <DataGridTemplateColumn Header="Toegelaten tot start">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <CheckBox IsChecked="{Binding allowed_to_start}"/>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
                <DataGridTemplateColumn Header="Uit koers genomen">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <CheckBox IsChecked="{Binding took_out_of_race}"/>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
            </DataGrid.Columns>
        </DataGrid>
    </Grid>

1 个答案:

答案 0 :(得分:2)

您可以在DataGrid上方放置一个额外的Grid,以获取额外的标题并绑定列宽:

        <StackPanel>
            <Grid>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="{Binding ElementName=A1, Path=ActualWidth, Mode=OneWay}"/>
                    <ColumnDefinition Width="{Binding ElementName=B1, Path=ActualWidth, Mode=OneWay}"/>
                    <ColumnDefinition Width="{Binding ElementName=C1, Path=ActualWidth, Mode=OneWay}"/>
                    <ColumnDefinition Width="{Binding ElementName=A2, Path=ActualWidth, Mode=OneWay}"/>
                    <ColumnDefinition Width="{Binding ElementName=B2, Path=ActualWidth, Mode=OneWay}"/>
                    <ColumnDefinition Width="{Binding ElementName=C2, Path=ActualWidth, Mode=OneWay}"/>
                    <ColumnDefinition Width="{Binding ElementName=A3, Path=ActualWidth, Mode=OneWay}"/>
                    <ColumnDefinition Width="{Binding ElementName=B3, Path=ActualWidth, Mode=OneWay}"/>
                    <ColumnDefinition Width="{Binding ElementName=C3, Path=ActualWidth, Mode=OneWay}"/>
                </Grid.ColumnDefinitions>
                <Label Grid.ColumnSpan="3" Grid.Column="0" Content="First dude"/>
                <Label Grid.ColumnSpan="3" Grid.Column="3" Content="Second dude"/>
                <Label Grid.ColumnSpan="3" Grid.Column="6" Content="Third dude"/>
            </Grid>
            <DataGrid>
                <DataGrid.Columns>
                    <DataGridTextColumn x:Name="A1" Header="First"/>
                    <DataGridTextColumn x:Name="B1" Header="B"/>
                    <DataGridTextColumn x:Name="C1" Header="C"/>
                    <DataGridTextColumn x:Name="A2" Header="First"/>
                    <DataGridTextColumn x:Name="B2" Header="B"/>
                    <DataGridTextColumn x:Name="C2" Header="C"/>
                    <DataGridTextColumn x:Name="A3" Header="First"/>
                    <DataGridTextColumn x:Name="B3" Header="B"/>
                    <DataGridTextColumn x:Name="C3" Header="C"/>
                </DataGrid.Columns>
            </DataGrid>
        </StackPanel>

当然,对于上下文菜单等,多余的标题将不会计入DataGrid。所有这些东西都必须在需要时手动添加。