我已经完成了大部分工作(例如,分组有效,但是所有组必须具有相同的级别)。
Product
Book
RecipeBook
Book#1
Book#2
ProgramingBook
Book#1
....
Disk
Disk
Disk#1
Disk#2
.....
我正在使用你的样本数据作为我的输入,因此我的类结构如下:
class Product
{
public string Name { get; set; }
}
class Book : Product
{
public string BookName { get; set; }
}
class RecipeBook : Book
{
public int NumRecipes { get; set; }
}
class ProgrammingBook : Book
{
public string LanguageCovered { get; set; }
}
class Disk : Product
{
public int Size { get; set; }
}
填充如下:
ObservableCollection<Product> products = new ObservableCollection<Product>();
products.Add(new ProgrammingBook() { BookName = "P1", LanguageCovered = "C#" });
products.Add(new ProgrammingBook() { BookName = "P2", LanguageCovered = "F#" });
products.Add(new RecipeBook() { BookName = "P3", NumRecipes = 4 });
products.Add(new RecipeBook() { BookName = "P4", NumRecipes = 6 });
products.Add(new Disk() { Size = 512 });
products.Add(new Disk() { Size = 1024 });
this.DataContext = products;
我们需要按类型对其进行分组,以便我们可以在CollectionViewSource PropertyGroupDescription上使用转换器。
这是我的转换器(我为不同级别的类型传递不同的值):
public class GroupByType : IValueConverter
{
public string type { get; set; }
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (type == "root")
{
if (value is Product)
{
return "Product";
}
return null;
}
if (type == "subs")
{
if (value is Book)
{
return "Book";
}
if (value is Disk)
{
return "Disk";
}
}
if (type == "main")
{
if (value is ProgrammingBook)
{
return "PBook";
}
if (value is RecipeBook)
{
return "RBook";
}
if (value is Disk)
{
return "Disk";
}
}
return null;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
一旦我们拥有所需的数据,我们需要适当的模板(我的模板很简单,在最后的XAML中显示),但我们需要某种选择它们的方式,因此我使用以下模板选择器:
public class Selector : DataTemplateSelector
{
public override DataTemplate SelectTemplate(object item, DependencyObject container)
{
string templateKey;
if (item is CollectionViewGroup)
{
if ((item as CollectionViewGroup).Name == null)
{
return null;
}
templateKey = "GroupTemplate";
}
else if (item is ProgrammingBook)
{
templateKey = "pTemplate";
}
else if (item is RecipeBook)
{
templateKey = "rTemplate";
}
else if (item is Disk)
{
templateKey = "dTemplate";
}
else
{
return null;
}
return (DataTemplate)((FrameworkElement)container).FindResource(templateKey);
}
}
我的XAML代码如下:
<Window.Resources>
<HierarchicalDataTemplate x:Key="GroupTemplate" ItemsSource="{Binding Path=Items}">
<TextBlock Text="{Binding Path=Name}" FontWeight="Bold"/>
</HierarchicalDataTemplate>
<DataTemplate x:Key="pTemplate">
<TextBlock Text="{Binding Path=LanguageCovered }"/>
</DataTemplate>
<DataTemplate x:Key="rTemplate">
<TextBlock Text="{Binding Path=NumRecipes}"/>
</DataTemplate>
<DataTemplate x:Key="dTemplate">
<TextBlock Text="{Binding Path=Size}"/>
</DataTemplate>
<c:GroupByType x:Key="gt" />
<c:Selector x:Key="s"/>
<CollectionViewSource Source="{Binding}" x:Key="cvs">
<CollectionViewSource.GroupDescriptions>
<PropertyGroupDescription>
<PropertyGroupDescription.Converter>
<c:GroupByType type="root"/>
</PropertyGroupDescription.Converter>
</PropertyGroupDescription>
<PropertyGroupDescription>
<PropertyGroupDescription.Converter>
<c:GroupByType type="subs"/>
</PropertyGroupDescription.Converter>
</PropertyGroupDescription>
<PropertyGroupDescription>
<PropertyGroupDescription.Converter>
<c:GroupByType type="main"/>
</PropertyGroupDescription.Converter>
</PropertyGroupDescription>
</CollectionViewSource.GroupDescriptions>
</CollectionViewSource>
</Window.Resources>
<Grid>
<TreeView ItemsSource="{Binding Source={StaticResource cvs}, Path=Groups}" ItemTemplateSelector="{StaticResource s}"/>
</Grid>
这里的想法是,对于你的层次结构中的每种类型,我们创建一个新的组,实际对象是树的叶节点。您可以通过向转换器添加新部分来添加尽可能多的分组级别。您还可以通过更改转换器的返回值来重命名组。
如果您有任何问题,请随时问,因为这是一个相当长的答案 :)