为什么我会对此进行研究呢?是因为我想看看,能否增强一下ListBox的分组功能,让他支持组内元素展开与收缩(类似于TreeView):
WPF 的 ItemsControl 天生支持Grouping(分类),据Pro WPF in C#2010所说:When you use grouping, your list creates a separate GroupItem object for each group, and it adds these GroupItem objects to the list. The GroupItem is a content control, so each GroupItem holds the appropriate container (like ListBoxItem objects) with your actual data. The secret to showing your groups is formatting the GroupItem element so it stands out. You could use a style that applies formatting to all the GroupItem objects in a list.
也就是说,启用了Grouping之后,会生成一些新的对象,GroupItem对象,那么如何启用Grouping呢?在我的测试下,发现,要启用Grouping并且使ListBox显示出Grouping的效果,要满足2个条件:
1)本身,DataSource要启用Grouping
2)ListBox的GroupStyle不能是null。
下面的就是具体测试与分析:
数据结构:
class Data
{
public string Value { get; set; }
public string Type { ge