Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/261.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将Treeview绑定到WPF中具有相同对象列表的对象_C#_Wpf_Binding_Treeview_Nodes - Fatal编程技术网

C# 将Treeview绑定到WPF中具有相同对象列表的对象

C# 将Treeview绑定到WPF中具有相同对象列表的对象,c#,wpf,binding,treeview,nodes,C#,Wpf,Binding,Treeview,Nodes,我正试着做一个关于树视图的报告。在这里,我可以编写一些文本作为父节点,如果需要,我会添加字符串作为子节点来显示更多信息 我做了一个报告: public class Report { private List<Report> reportList = new List<Report>(); public string Text { get; set; } public List<Report> Reports {

我正试着做一个关于树视图的报告。在这里,我可以编写一些文本作为父节点,如果需要,我会添加字符串作为子节点来显示更多信息

我做了一个报告:

public class Report
{
    private List<Report> reportList = new List<Report>();

    public string Text { get; set; }
    public List<Report> Reports
    {
        get
        {
            return reportList;
        }
        set
        {
            reportList = value;
        }
    }
}
公共类报告
{
私有列表reportList=新列表();
公共字符串文本{get;set;}
公开名单报告
{
得到
{
返回报告列表;
}
设置
{
报告列表=值;
}
}
}
然后我会有一个报告集合,然后我会将它输入到一个treeview itemssource,但是我不知道如何创建绑定,以便它能够正确地创建子节点。但是,我尝试搜索google,但找不到正确的方法

这里有人能帮我吗?

你需要使用一个




噢,天哪……我以为我已经这么做了,但我想我一定是把绑定搞砸了什么的。非常感谢。我可以问一个后续问题吗?假设我有一个人对象,他有:名字,姓氏,地址,性别,身高。在父节点中,您只想显示名字和姓氏,但在子节点中,它们拥有所有信息(排序的主详细信息),我不能使用分层数据,对吗?@Xenoxsis:而不是使用
数据类型
属性根据树中元素的类型选择
层次数据模板
(这不区分不同级别上相同类型的元素)可以使用
ItemTemplate
属性为下一级别指定另一个
hierarchycaldatatemplate
<TreeView ItemsSource="{Binding Reports}">
    <TreeView.Resources>
        <HierarchicalDataTemplate DataType="{x:Type local:Report}" ItemsSource="{Binding Reports}">
            <TextBlock Text="{Binding Text}" />
        </HierarchicalDataTemplate>
    </TreeView.Resources>
</TreeView>
<TreeView ItemsSource="{Binding ReportCollection}">
  <TreeView.ItemTemplate>
    <HierarchicalDataTemplate ItemsSource="{Binding Reports}">
      <TextBlock Text="{Binding Text}" />
    </HierarchicalDataTemplate>
  </TreeView.ItemTemplate>
</TreeView>