如果您为数据创建了一个CollectionViewsource,那么可以在集合GroupDescriptions中使用PropertyGroupDescription将所有数据按照所需属性进行分组。
然后在DataGrid中,您可以创建一个GroupStyle来显示TextBlock或其他内容,以便将所有组在DataGrid中分开显示。
以下是完整的演示示例,因为这样更容易展示而不是解释:)
Xaml:
<Window x:Class="WpfApplication20.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="505" Width="525" Name="UI">
<Window.Resources>
<!--Create CollectionViewSource and set the property you want to group by-->
<CollectionViewSource x:Key="MyItems" Source="{Binding Items, ElementName=UI}">
<CollectionViewSource.GroupDescriptions>
<PropertyGroupDescription PropertyName="Category" />
</CollectionViewSource.GroupDescriptions>
</CollectionViewSource>
</Window.Resources>
<Grid>
<DataGrid ItemsSource="{Binding Source={StaticResource MyItems}}">
<DataGrid.GroupStyle>
<GroupStyle>
<GroupStyle.HeaderTemplate>
<DataTemplate>
<StackPanel>
<!--Name property is the GroupName created by the CollectionViewSource-->
<TextBlock Text="{Binding Path=Name}" Margin="10,0,0,0" FontSize="18" FontWeight="Bold"/>
</StackPanel>
</DataTemplate>
</GroupStyle.HeaderTemplate>
</GroupStyle>
</DataGrid.GroupStyle>
</DataGrid>
</Grid>
</Window>
代码:
namespace WpfApplication20
{
public partial class MainWindow : Window
{
private ObservableCollection<MyDataObject> _items = new ObservableCollection<MyDataObject>();
public MainWindow()
{
InitializeComponent();
Items.Add(new MyDataObject { Category = "IT", ProductName = "Stackoverflow", Description = "Group", Price = "Demo" });
Items.Add(new MyDataObject { Category = "Monitors", ProductName = "Stackoverflow", Description = "Group", Price = "Demo" });
Items.Add(new MyDataObject { Category = "Monitors", ProductName = "Stackoverflow", Description = "Group", Price = "Demo" });
Items.Add(new MyDataObject { Category = "Monitors", ProductName = "Stackoverflow", Description = "Group", Price = "Demo" });
Items.Add(new MyDataObject { Category = "HDD", ProductName = "Stackoverflow", Description = "Group", Price = "Demo" });
Items.Add(new MyDataObject { Category = "HDD", ProductName = "Stackoverflow", Description = "Group", Price = "Demo" });
}
public ObservableCollection<MyDataObject> Items
{
get { return _items; }
set { _items = value; }
}
}
public class MyDataObject
{
public string Category { get; set; }
public string ProductName { get; set; }
public string Description { get; set; }
public string Price { get; set; }
}
}
结果:
增强功能
在DataGrid
的ContainerStyle
中重写GroupItem
模板以使用Expander
可能是一个不错的想法,这样你就可以展开和折叠组。
例如:
<DataGrid ItemsSource="}" CanUserAddRows="False">
<DataGrid.GroupStyle>
<GroupStyle>
<GroupStyle.ContainerStyle>
<Style TargetType="">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="">
<Expander Name="expander">
<Expander.Header>
<StackPanel >
<TextBlock Text="" FontWeight="Bold"/>
<TextBlock Text="Items: }" FontSize="9" />
</StackPanel>
</Expander.Header>
<ItemsPresenter />
</Expander>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</GroupStyle.ContainerStyle>
</GroupStyle>
</DataGrid.GroupStyle>
</DataGrid>
结果:
(该图片未提供说明)
TreeView
。 - Adi Lester