C# 无集合的TreeView递归

C# 无集合的TreeView递归,c#,wpf,templates,treeview,C#,Wpf,Templates,Treeview,如果数据是按集合组织的,则可以使用HierarchycalDataTemplates填充树视图。但我有一些数据,保存在几个众所周知的类中,需要在树视图中显示。下面是一个代码示例: using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Collections.ObjectModel; namespace Test { public class G

如果数据是按集合组织的,则可以使用HierarchycalDataTemplates填充树视图。但我有一些数据,保存在几个众所周知的类中,需要在树视图中显示。下面是一个代码示例:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;

namespace Test
{
    public class GroupWithList
    {
        public string Name { get; set; }

        public ObservableCollection<string> Items { get; set; }

        public GroupWithList(string name)
        {
            this.Name = name;
            this.Items = new ObservableCollection<string>();
        }
    }

    public class GroupWith2Children
    {
        public string Name { get; set; }

        public ObservableCollection<GroupWithList> Groups { get; set; }

        public GroupWithList First
        {
            get
            {
                return this.Groups[0];
            }
        }

        public GroupWithList Second
        {
            get
            {
                return this.Groups[1];
            }
        }

        public GroupWith2Children()
        {
            this.Name = "GroupWith2Children";
            this.Groups = new ObservableCollection<GroupWithList>();

            GroupWithList g;

            g = new GroupWithList("Letters");
            g.Items.Add("u");
            g.Items.Add("a");
            g.Items.Add("t");
            this.Groups.Add(g);

            g = new GroupWithList("Numbers");
            g.Items.Add("12");
            g.Items.Add("153");
            this.Groups.Add(g);
        }
    }
}

public partial class MainWindow : Window
{
    public static readonly DependencyProperty ItemsProperty = DependencyProperty.Register("Items", typeof(ObservableCollection<GroupWith2Children>), typeof(MainWindow), new FrameworkPropertyMetadata(new ObservableCollection<GroupWith2Children>()));

    public ObservableCollection<GroupWith2Children> Items
    {
        get
        {
            return (ObservableCollection<GroupWith2Children>)this.GetValue(ItemsProperty);
        }

        set
        {
            this.SetValue(ItemsProperty, value);
        }
    }

    public MainWindow()
    {
        InitializeComponent();

        this.Items.Add(new GroupWith2Children());
        this.Items.Add(new GroupWith2Children());
    }
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Collections.ObjectModel;
名称空间测试
{
公共类GroupWithList
{
公共字符串名称{get;set;}
公共ObservableCollection项{get;set;}
公共GroupWithList(字符串名称)
{
this.Name=Name;
this.Items=新的ObservableCollection();
}
}
有2个孩子的公共班级小组
{
公共字符串名称{get;set;}
公共可观测集合组{get;set;}
公共组优先使用列表
{
得到
{
返回此。组[0];
}
}
第二个公共组
{
得到
{
返回此。组[1];
}
}
有2个孩子的公共团体()
{
this.Name=“GroupWith2Children”;
this.Groups=新的ObservableCollection();
g组;
g=新的分组列表(“字母”);
g、 项目。添加(“u”);
g、 项目。添加(“a”);
g、 项目。添加(“t”);
本.组.加(g);
g=新的GroupWithList(“数字”);
g、 项目。添加(“12”);
g、 项目。添加(“153”);
本.组.加(g);
}
}
}
公共部分类主窗口:窗口
{
public static readonly dependencProperty ItemsProperty=dependencProperty.Register(“Items”、typeof(observedcollection)、typeof(main window)、new FrameworkPropertyMetadata(new observedcollection());
公共可观测收集项目
{
得到
{
返回(ObservableCollection)this.GetValue(ItemsProperty);
}
设置
{
此.SetValue(ItemsProperty,value);
}
}
公共主窗口()
{
初始化组件();
this.Items.Add(新的GroupWith2Children());
this.Items.Add(新的GroupWith2Children());
}
}
下面是xaml代码:

<Window x:Class="Test.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:sys="clr-namespace:System;assembly=mscorlib"
        xmlns:test="clr-namespace:Test"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <Grid.Resources>

            <DataTemplate DataType="{x:Type test:GroupWith2Children}">
                <TreeViewItem Header="{Binding Mode=OneWay, Path=Name}">
                    <TreeViewItem Header="First" ItemsSource="{Binding Mode=OneWay, Path=First.Items}"/>
                    <TreeViewItem Header="Second" ItemsSource="{Binding Mode=OneWay, Path=Second.Items}"/>
                </TreeViewItem>
            </DataTemplate>

            <HierarchicalDataTemplate DataType="{x:Type test:GroupWithList}" ItemsSource="{Binding Path=Items}">
                <TextBlock Text="GroupWithList"/>
            </HierarchicalDataTemplate>

            <DataTemplate DataType="{x:Type sys:String}">
                <TextBlock Text="{Binding}"/>
            </DataTemplate>
        </Grid.Resources>

        <TreeView>
            <TreeViewItem Header="Root" ItemsSource="{Binding Mode=OneWay, RelativeSource={RelativeSource AncestorType={x:Type Window}}, Path=Items}"/>
        </TreeView>
    </Grid>
</Window>

该示例工作正常,但您不能选择在GroupWith2Children的DataTemplate中生成的项。树视图似乎将整个模板视为一个项目。您只能选择根节点和整个子树“GroupWith2Children”


用集合替换GroupWith2Children不是我的选择。有什么想法吗,我做错了什么?

你不必用一个集合取代
组,用2个孩子取代
;您可以实现可枚举属性。如果您根本无法使用
GroupWith2Children
类,请实现一个包装器类:

public class GroupWrapper()
{
   public GroupWrapper(GroupWith2Children group)
   {
      Group = group;
   }

   public GroupWith2Children Group { get; private set; ]

   public IEnumerable<GroupWithList> Groups
   {
      get
      {
         yield return Group.First;
         yield return Group.Second;
      }
   }
}
public类GroupWrapper()
{
公共GroupWrapper(GroupWith2Children组)
{
组=组;
}
public GroupWith2Children组{get;private set;]
公共可数群
{
得到
{
收益率-收益率组;
收益率-收益率组;
}
}
}

可以工作,但我必须使用IEnumerable(它有一个列表和名称的引用)来显示硬编码的名称(“Group1”和“Group2”)。谢谢