Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/257.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# 绑定包含项目列表的项目列表_C#_Wpf_List_Binding_Observablecollection - Fatal编程技术网

C# 绑定包含项目列表的项目列表

C# 绑定包含项目列表的项目列表,c#,wpf,list,binding,observablecollection,C#,Wpf,List,Binding,Observablecollection,我有我的班级: public class Comment { ... public List<Comment> Children; } 公共类注释 { ... 公开儿童名单; } 在我的ViewModel中,我有一个带有注释的ObservableCollection,绑定正在工作,但我也希望绑定子对象 以下是我的看法: <ListBox ItemsSource="{Binding Comments}" ItemTemplate="{Bin

我有我的班级:

public class Comment
{
    ...
    public List<Comment> Children;
}
公共类注释
{
...
公开儿童名单;
}
在我的ViewModel中,我有一个带有注释的ObservableCollection,绑定正在工作,但我也希望绑定子对象

以下是我的看法:

<ListBox 
    ItemsSource="{Binding Comments}" 
    ItemTemplate="{Binding Source={StaticResource comment}}">
</ListBox>

该模板是内容和子项的每个注释的UserControl绑定:

<Grid>
    <TextBlock Text="{Binding Body}" />

    <ListBox 
        ItemsSource="{Binding Children}" 
        ItemTemplate="{Binding Source={StaticResource comment}}">
    </ListBox>
</Grid>

显然,我不能绑定子级,因为我需要一个ObservableCollection,但我不知道如何做,如果我在注释类中用ObservableCollection替换列表,它也不起作用

现在,我只在ViewModel的列表中列出我的注释,但我希望为每个孩子递归地这样做


谢谢这棵树有多深?子注释是否可以具有任意级别的子注释?在这种情况下,您将看到一个分层数据结构,并使用。下面是一个相当直接的示例:,或者MSDN文章:

如果这只是一个简单的两级深层结构,那么就有两个类:commodate和childcommodate,您的方法就会奏效


无论如何,Comment似乎是一个模型类。也可以使用CommentVm,以及可观察的子评论集合。使用linq扩展功能选择Perfect的可能副本,创建一个可观察收集列表,这就是我要找的!谢谢