c# - 如何在我的 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"));
然后,您可以像绑定任何其他集合一样绑定到它。
推荐阅读
- json - Scala 上的 Play2:JSON 序列化/反序列化
- python - 带有 SelectField 的 Flask WTForm 不适用于 form.validate_on_submit
- codenameone - 谷歌地图 CodeName1 NullPointerException
- database - Laravel 数据库实战
- python - Discord.py - Cog 中的成员获取
- android - Android Studio:卡片视图
- c++ - 自定义节流器中的无限循环场景(竞争条件?)
- intellij-idea - ClassNotFoundException: gherkin.formatter.Formatter
- jquery - 如何在查询字符串上调用 serializeArray
- mysql - sql 查询 - 如何创建 2 个级别的计数和组