Rahul W
Rahul W

Reputation: 841

HierarchialDataTemplate for adding mixed data to treeview

Hi I have a tree view to display some hierarchical data. My classes were as follows

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> { };

I have created some test data using those classes.

        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;

The XAML is as follows

        <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>

The problem is i am not able to display the data down to the last sub folder level. I have seen many questions in wpf, but, most of them have same classes in the collection or nested collections of same type.

I may not be able to change the classes to return CompositeCollection as this is how i receive data from the Data layer.

I am looking at a structure like

User0001
  Case0001
     Main Folder
       Sub Folder
          sub...etc...
User0002
etc....

above xaml, only shows me UserId from the Resonse class.

Upvotes: 0

Views: 187

Answers (1)

Nitin Purohit
Nitin Purohit

Reputation: 18580

you will have to update your templates like below:

      <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>

Thanks

Upvotes: 1

Related Questions