【问题标题】:How to group child objects from one List in a TreeView?如何从 TreeView 中的一个列表中对子对象进行分组?
【发布时间】:2013-07-03 17:45:07
【问题描述】:

我如何获取一个列表,然后在 TreeView 中按它们的类对它们进行分组?我读了this,但它没有对子对象进行分组,它只是在集合中具有相关属性。

  • 仓库
    • A 型
      • 亚型 1
      • 亚型 2
    • B 型
    • C 型

如果我有包含所有这些类型的列表,如何在 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 型

【问题讨论】:

    标签: c# wpf treeview hierarchicaldatatemplate


    【解决方案1】:

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

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

    或者您可以在 XAML 中通过显式创建 CollectionViewSource 来实现。

    <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

    【讨论】:

    • Property 分组,而不是按对象类型本身。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-24
    • 1970-01-01
    • 1970-01-01
    • 2018-05-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多