如何在我的 DataGrid 中合并相同的值?

How Can I merge the same Value in my DataGrid?

所以我制作了一个 DataGrid,我在其中使用一些模板为整个 DataGrid 设置了样式。现在我想合并每个具有相同值的单元格。我从 SQL 查询中添加数据。那是我的 DataGrid XAML 代码:

<DataGrid.Resources>
                    <!--Design kopfzeile-->
                    <Style TargetType="{x:Type DataGridColumnHeader}" x:Name="test" >
                        <Setter Property="Background" Value="Red"/>
                        <Setter Property="Foreground" Value="LightBlue"/>
                        <Setter Property="FontWeight" Value="SemiBold"/>
                        <Setter Property="Height" Value="30"/>
                        <Setter Property="FontSize" Value="15"/>
                        <Setter Property="BorderThickness" Value="0,0,2,0" />
                        <Setter Property="BorderBrush" Value="#333333"/>
                        <Setter Property="Padding" Value="10 0 0 0"/>
                        <Setter Property="Template">
                            <Setter.Value>
                                <ControlTemplate TargetType="{x:Type DataGridColumnHeader}">
                                    <Grid x:Name="insideHeader" Background="#353E4A">
                                        <Border x:Name="borderHeader" BorderThickness="1"
                                        CornerRadius="6"
                                        Background="#4F5C73"
                                        Padding="10,0,0,0"
                                        Margin="2">
                                            <ContentPresenter/>
                                        </Border>

                                        <Thumb x:Name="PART_RightHeaderGripper" Grid.Column="1"
                        HorizontalAlignment="Right"
                        Width="2" BorderThickness="1"
                        BorderBrush="#353E4A"
                        Cursor="SizeWE"/>

                                    </Grid>

                                    <ControlTemplate.Triggers>
                                        <DataTrigger  Binding="{Binding ElementName=toogleButton,Path=IsChecked}" Value="False">
                                            <Setter TargetName="borderHeader" Property="Background" Value="#FA9F34"/>
                                            <Setter Property="Foreground" Value="#2B2B2B"/>
                                            <Setter TargetName="insideHeader" Property="Background" Value="#00336E"/>
                                        </DataTrigger>
                                        <Trigger Property="IsMouseOver" Value="True">
                                            <Setter TargetName="borderHeader" Property="Background" Value="#4182C6"/>
                                        </Trigger>
                                    </ControlTemplate.Triggers>
                                </ControlTemplate>
                            </Setter.Value>
                        </Setter>
                    </Style>

                    <!--Deaktivieren Des rowheader-->
                    <Style TargetType="{x:Type DataGridRowHeader}">
                        <Setter Property="Background" Value="Transparent"/>
                    </Style>

                    <!--Cellen Design-->
                    <Style TargetType="{x:Type DataGridCell}">
                        <Setter Property="Background" Value="#3E4659"/>
                        <Setter Property="Foreground" Value="LightBlue"/>
                        <Setter Property="BorderThickness" Value="0,0,2,0" />
                        <Setter Property="BorderBrush" Value="#333333"/>
                        <Setter Property="Template">
                            <Setter.Value>
                                <ControlTemplate TargetType="{x:Type DataGridCell}">
                                    <Border x:Name="insideBorder" Background="#353E4A">
                                        <Border x:Name="BorderCell" BorderThickness="1"
                                        CornerRadius="6"
                                        Background="#4F5C73"
                                        Padding="10,0,0,0"
                                        Margin="2">
                                            <ContentPresenter/>
                                        </Border>
                                    </Border>
                                    <ControlTemplate.Triggers>
                                        <DataTrigger  Binding="{Binding ElementName=toogleButton,Path=IsChecked}" Value="False">
                                            <Setter TargetName="BorderCell" Property="Background" Value="#0051B0"/>
                                            <Setter TargetName="insideBorder" Property="Background" Value="#00336E"/>
                                        </DataTrigger>
                                        <Trigger Property="IsMouseOver" Value="True">
                                            <Setter TargetName="BorderCell" Property="Background" Value="#4182C6"/>
                                        </Trigger>
                                    </ControlTemplate.Triggers>
                                </ControlTemplate>
                            </Setter.Value>
                        </Setter>


                    </Style>
                </DataGrid.Resources>

我已经尝试过 是类似的东西,使用 GroupStyles 但没有用,使用该代码没有任何反应:

<DataGrid ItemsSource="{Binding GroupedData}" AutoGenerateColumns="False" MinRowHeight="25" CanUserAddRows="False" CanUserDeleteRows="False">
            <DataGrid.GroupStyle>
                <!-- First Group -->
                <GroupStyle ContainerStyle="{StaticResource GroupItemStyle}">
                    <GroupStyle.Panel>
                        <ItemsPanelTemplate>
                            <DataGridRowsPresenter/>
                        </ItemsPanelTemplate>
                    </GroupStyle.Panel>
                </GroupStyle>

                <!-- Second Group -->
                <GroupStyle ContainerStyle="{StaticResource GroupItemStyle}">
                    <GroupStyle.Panel>
                        <ItemsPanelTemplate>
                            <DataGridRowsPresenter/>
                        </ItemsPanelTemplate>
                    </GroupStyle.Panel>

                </GroupStyle>

                <!-- Third Group -->
                <GroupStyle ContainerStyle="{StaticResource GroupItemStyle}">
                    <GroupStyle.Panel>
                        <ItemsPanelTemplate>
                            <DataGridRowsPresenter/>
                        </ItemsPanelTemplate>
                    </GroupStyle.Panel>
                </GroupStyle>
            </DataGrid.GroupStyle>
            <DataGrid.Columns>
    ...
            </DataGrid.Columns>
        </DataGrid>

这里是样式:

<Style x:Key="GroupItemStyle" TargetType="{x:Type GroupItem}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type GroupItem}">
                    <StackPanel Orientation="Horizontal" >
                        <Border BorderThickness="0">
                            <Grid HorizontalAlignment="Center" VerticalAlignment="Center">
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="Auto"/>
                                    <ColumnDefinition Width="Auto"/>
                                </Grid.ColumnDefinitions>

                                <Border BorderThickness="1" MinWidth="150">
                                    <Grid HorizontalAlignment="Center" VerticalAlignment="Center">
                                        <ContentPresenter Content="{Binding Name}" >
                                        </ContentPresenter>
                                    </Grid>

                                </Border>
                                <Border BorderThickness="0" Grid.Column="1">
                                    <Grid HorizontalAlignment="Center" VerticalAlignment="Center">
                                        <ItemsPresenter/>
                                    </Grid>
                                </Border>
                            </Grid>
                        </Border>
                    </StackPanel>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

我尝试得到的结构看起来像帽子结构:

那么为什么这个不起作用?

根据您的评论,我了解到您的问题具体是 DataGrid 分组不适合您。

如果要使用 DataGrid 分组,则必须使用 CollectionViewSource,为其提供 GroupDescription 并将 CollectionViewSource 指向它应该使用的集合。绑定时,您然后绑定到 CollectionViewSource 而不是实际的集合。如果 DataGrid 没有这样的 GroupDescription,GroupStyle 将被忽略。

例如

<Window.Resources>
   <CollectionViewSource x:Key="GroupedDataViewSource" Source="{Binding GroupedData}">
        <CollectionViewSource.GroupDescriptions>
            <PropertyGroupDescription PropertyName="Country"/>
        </CollectionViewSource.GroupDescriptions>
    </CollectionViewSource>
</Window.Resources>

并绑定到它...

<DataGrid ItemsSource="{Binding Source={StaticResource ResourceKey = GroupedDataViewSource}}"/>

在 C# 中创建 ViewSource:

CollectionViewSource myViewSource = new CollectionViewSource { Source = mySource };
myViewSource.GroupDescriptions.Add(new PropertyGroupDescription("someHeaderNameToGroupBy"));

然后您可以像绑定任何其他集合一样绑定到它。