如何在TreeView中对一个List中的子对象进行分组?

时间:2013-07-03 17:45:08

标签: c# wpf treeview hierarchicaldatatemplate

如何获取List,然后在TreeView组中按类进行操作?我读了this,但它没有对子对象进行分组,而是只在集合中有相关的属性。

  • 仓库
    • A型
      • 子类型1
      • 子类型2
    • B型
    • C型

如果我有List中包含所有类型的List,如何在TreeView中显示分组?

之前,我手动添加它们,一次一个TreeViewItem。

List<Warehouse> myWarehouse = new List<Warehouse>();
TreeViewItem WarehouseNode = new TreeViewItem() { Header = "Warehouse" };
TreeViewItem TypeANode = new TreeViewItem() { Header = "Type A" };
foreach(SubtypeA type in myWarehouse) {
    TypeANode.Items.Add(new TreeViewItem() { Header = type.Name };
}
WarehouseNode.Items.Add(TypeANode);
etc. for Type B and Type C.

我正在阅读HierarchicalDataTemplate,看起来这就是我想要的方式,使用ItemTemplateSelector更改DataTemplate。

所以,我开始使用HierarchicalDataTemplates并提出了以下内容。

XAML:

<Window.Resources>
    <Utility:MyTemplateSelector x:Key="MyTemplateSelector" />
    <HierarchicalDataTemplate x:Key="TypeCTemplate" DataType="{x:Type EntityType:TypeC}"
                              ItemsSource="{Binding OBJS}" ItemTemplateSelector="{StaticResource MyTemplateSelector}">
        <TextBlock Text="Type C"/>
    </HierarchicalDataTemplate>
    <HierarchicalDataTemplate x:Key="TypeBTemplate" DataType="{x:Type EntityType:TypeB}"
                              ItemsSource="{Binding OBJS}" ItemTemplateSelector="{StaticResource MyTemplateSelector}">
        <TextBlock Text="Type B"/>
    </HierarchicalDataTemplate>
    <HierarchicalDataTemplate x:Key="TypeATemplate" DataType="{x:Type EntityType:TypeA}"
                              ItemsSource="{Binding OBJS}" ItemTemplateSelector="{StaticResource MyTemplateSelector}">
        <TextBlock Text="Type A"/>
    </HierarchicalDataTemplate>
</Window.Resources>
<Grid>
    <TreeView Name="MyTreeView" ItemsSource="{Binding OBJS}" 
              ItemTemplateSelector="{StaticResource MyTemplateSelector}">
    </TreeView>
</Grid>

我写了DataTemplateSelector

class MyTemplateSelector : DataTemplateSelector {
    public override DataTemplate SelectTemplate(object item, System.Windows.DependencyObject container) {
        MethodInfo mi = container.GetType().GetMethod("FindResource") as MethodInfo;
        if(mi != null) {
            string strItem = item.ToString().Split('.').Last();
            switch(item.ToString().Split('.').Last()) {
                case "Type A":
                    return mi.Invoke(container, new object[] { "TypeATemplate" }) as DataTemplate;
                case "Type B":
                    return mi.Invoke(container, new object[] { "TypeBTemplate" }) as DataTemplate;
                case "Type C":
                    return mi.Invoke(container, new object[] { "TypeCTemplate" }) as DataTemplate;
            }

            return null;
        }

        return null;
    }
}

但现在,当我查看TreeView时,它给了我这个:

  • B型
  • B型
  • B型
  • C型

1 个答案:

答案 0 :(得分:0)

通过在其CollectionViewSource上设置GroupDescriptions来对您的收藏进行分组。您可以通过执行以下操作在代码中执行此操作:

CollectionViewSource.GetDefaultView(yourCollection).GroupDescriptions.Add(
new PropertyGroupDescription("PropertyName"));

或者您可以通过显式创建CollectionViewSource在XAML中执行此操作。

<CollectionViewSource
    Source="{StaticResource yourCollection}"
    xmlns:dat="clr-namespace:System.Windows.Data;assembly=PresentationFramework">
    <CollectionViewSource.GroupDescriptions>
        <dat:PropertyGroupDescription PropertyName="PropertyName"/>
    </CollectionViewSource.GroupDescriptions>
</CollectionViewSource>

另请查看enter link description here