Home > Blockchain >  Flattened list of child and parents to a tree view hierarchy
Flattened list of child and parents to a tree view hierarchy

Time:10-02

I have a list of objects that looks like this:

  public class Folders
     {
         public string name { get; set; }
         public string description { get; set; }
         public string path { get; set; }
         public string id { get; set; }
         public AccessType accessType { get; set; }
     }

These objects were all parsed from a JSON that is quite an ugly one and I've managed to flatten the list down to this.

The problem that I have is that I need to create a Tree View using WPF, but now I have issues creating the hierarchy into the Tree. The path property has the path to that certain folder. Here is the JSON.

{
  "name": "DataRoot",
  "description": "Root data directory node",
  "path": "DataRoot",
  "id": "00000000-0000-0000-0000-000000000000",
  "children": [
    {
      "name": "Second folder",
      "description": "Second folder",
      "path": "Second folder",
      "id": "00000000-0000-0000-0000-000000000001",
      "children": [
        {
          "name": "Third Folder",
          "description": "Third Folder",
          "path": "Second folder/Third Folder",
          "id": "00000000-0000-0000-0000-000000000002",
          "children": [
            {
              "name": "Fourth Folder",
              "description": "Fourth Folder",
              "path": "Second folder/Third Folder/Fourth Folder",
              "id": "00000000-0000-0000-0000-000000000003",
              "accessType": {
                "read": true,
                "write": true
              },
              "children": []
            }
          ]
        },
        {
          "name": "Fifth Folder",
          "description": "Review Required",
          "path": "Second folder/Fifth Folder,
          "id": "00000000-0000-0000-0000-000000000003",
          "children": []
        },
        {
          "name": "Sixth Folder",
          "description": "Sixth Folder",
          "path": "Second folder/Sixth Folder",
          "id": "00000000-0000-0000-0000-000000000004",
          "children": [
            {
              "name": "Seventh Folder",
              "description": "Seventh Folder",
              "path": "Second folder/Sixth Folder/Seventh Folder",
              "id": "00000000-0000-0000-0000-000000000005",
              "children": []
            }
          ]
        }
      ]
    }
  ]
}

I can't seem to develop an algorithm to make the hierarchy in the tree folder... I was thinking to make good use of the path property, but I still can't find a good way to use it so that I can make the tree look as it should. My list is flattened as I said, but now I don't know what will be a good algorithm to develop the hierarchy.

Thanks and please help :)

CodePudding user response:

I need to create a Tree View using WPF

One has to enumerate the json nodes (JsonElement if using System.Text.Json) and add them into the WPF TreeView by creating TreeViewItems based on what level of nodes one finds oneself at the time.

SO is not a place to quickly answer this question.


I have written a basic WPF JSON Treeview as a .Net Core WPF control that can be installed as a Nuget Package. Once installed see if it flattens the look as you want.

If not, look at my Github repository to see if an example for you to base your work off of. (Do you have an update to the code to suggest, please join the project and put in a PR).

Example Code

  • Related