如何在我的 DataGrid 中合并相同的值?
Posted
技术标签:
【中文标题】如何在我的 DataGrid 中合并相同的值?【英文标题】:How Can I merge the same Value in my DataGrid? 【发布时间】:2021-12-17 17:43:39 【问题描述】:所以我制作了一个 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 行放置在 DataGrid 的单独样式子集中?我只是假设后者,因为您正在尝试使用分组。尽管您向我们展示了很多样式,但我们对基础数据的结构一无所知。我可以告诉你为什么你的分组可能不起作用,但我不知道分组是否真的是你正在寻找的。span> 我添加了一张我想要得到什么结构的图片 这能回答你的问题吗? Merge Cells in WPF DataGrid @Shrimperator 我已经试过了,但是没用 【参考方案1】:从您的 cmets 中,我了解到您的具体问题是,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"));
然后您可以像绑定任何其他集合一样绑定到它。
【讨论】:
如何仅使用 c# 代码实现相同的功能,没有 xaml 文件?在 form.designer.cs 类中 我不知道“form.designer.cs 类”是什么意思,但我已经编辑了答案以解释如何在 C# 中创建 GrooupDescriptions 和 ViewSources。以上是关于如何在我的 DataGrid 中合并相同的值?的主要内容,如果未能解决你的问题,请参考以下文章
Excle如何将多个同ID的值,合并到一个单元格内,并以逗号隔开
如何在我的 winforms 应用程序中设置我的 datagrid 滚动条的位置?
如何在我的 DataGrid 中获取所选单元格的当前列(C# WPF 应用程序)