HierarchialDataTemplate用于向树视图添加混合数据

时间:2013-09-08 06:06:13

标签: c# wpf xaml binding hierarchicaldatatemplate

您好我有一个树视图来显示一些分层数据。我的课程如下

public class Response
{
    public string UserId { get; set; }
    public CaseItems Cases { get; set; }
}

public class CaseItem
{
    public int ID { get; set; }
    public string Name { get; set; }
    public CaseFolders Folders { get; set; }
}

public class CaseFolder
{
    public int FolderID { get; set; }
    public string FolderName { get; set; }
    public CaseItem Case { get; set; }

    public CaseFolders Folders { get; set; }
}

public class CaseItems : List<CaseItem> { };

public class CaseFolders : List<CaseFolder> { };

public class Responses : List<Response> { };

我已经使用这些类创建了一些测试数据。

        Responses ResponseList = new Responses();
        Response response = new Response();
        response.Cases = new CaseItems();
        response.UserId = "USER 0001";
        ResponseList.Add(response);

        CaseItem Case = new CaseItem();
        Case.Folders = new CaseFolders();
        Case.Name = "CASE0001";
        Case.ID = 1;
        response.Cases.Add(Case);

        CaseFolder MainFolder = new CaseFolder();
        MainFolder.FolderID = 1;
        MainFolder.FolderName = "Case Folder";
        MainFolder.Folders = new CaseFolders();
        MainFolder.Case = Case;
        Case.Folders.Add(MainFolder);

        CaseFolder SubFolder = new CaseFolder();
        SubFolder.FolderID = 2;
        SubFolder.FolderName = "Sub Folder";
        SubFolder.Case = Case;
        MainFolder.Folders.Add(SubFolder);

        Response AnotherResponse = new Response();
        AnotherResponse.Cases = new CaseItems();
        AnotherResponse.Cases.Add(Case);
        AnotherResponse.UserId = "USER 0002";
        ResponseList.Add(AnotherResponse);

        tvTest.DataContext = ResponseList;

XAML如下

        <TreeView Name="tvTest"
              ItemsSource="{Binding}">
        <TreeView.Resources>
            <HierarchicalDataTemplate DataType="{x:Type local:Response}"
                                      ItemsSource="{Binding}">
                <Label Content="{Binding UserId}"/>
            </HierarchicalDataTemplate>
            <HierarchicalDataTemplate DataType="{x:Type local:CaseItem}"
                                      ItemsSource="{Binding Cases}">
                <Label Content="{Binding Path=Name}"/>
            </HierarchicalDataTemplate>
            <HierarchicalDataTemplate DataType="{x:Type local:CaseFolder}"
                                      ItemsSource="{Binding ????}">
                <Label Content="{Binding Path=FolderName}"/>
            </HierarchicalDataTemplate>


        </TreeView.Resources>
    </TreeView>

问题是我无法将数据显示到最后一个子文件夹级别。 我在wpf中看到过很多问题,但是,大多数问题在集合中都有相同的类或者是同一类型的嵌套集合。

我可能无法更改类以返回CompositeCollection,因为这是我从数据层接收数据的方式。

我正在寻找像

这样的结构
User0001
  Case0001
     Main Folder
       Sub Folder
          sub...etc...
User0002
etc....

在xaml之上,只显示Resonse类的UserId。

1 个答案:

答案 0 :(得分:1)

您必须更新模板,如下所示:

      <TreeView Name="tvTest"
              ItemsSource="{Binding}">
            <TreeView.Resources>
                <HierarchicalDataTemplate DataType="{x:Type local:Response}"
                                      ItemsSource="{Binding Cases}">
                    <Label Content="{Binding UserId}"/>
                </HierarchicalDataTemplate>
                <HierarchicalDataTemplate DataType="{x:Type local:CaseItem}"
                                      ItemsSource="{Binding Folders}">
                    <Label Content="{Binding Path=Name}"/>
                </HierarchicalDataTemplate>
                <HierarchicalDataTemplate DataType="{x:Type local:CaseFolder}"
                                      ItemsSource="{Binding Folders}">
                    <Label Content="{Binding Path=FolderName}"/>
                </HierarchicalDataTemplate>


            </TreeView.Resources>
        </TreeView>

由于