[英]Flattened list of child and parents to a tree view hierarchy
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.这些对象都是从一个非常丑陋的 JSON 中解析出来的,我已经设法将列表缩小到这个。
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.我遇到的问题是我需要使用 WPF 创建一个树视图,但是现在我在将层次结构创建到树中时遇到了问题。 The
path
property has the path to that certain folder. path
属性具有该特定文件夹的路径。 Here is the JSON.这是 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.我似乎无法开发一种算法来制作树文件夹中的层次结构......我正在考虑充分利用
path
属性,但我仍然找不到使用它的好方法,以便我可以制作树看起来应该是这样。 My list is flattened as I said, but now I don't know what will be a good algorithm to develop the hierarchy.正如我所说,我的列表是扁平的,但现在我不知道什么是开发层次结构的好算法。
I need to create a Tree View using WPF
我需要使用 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.必须枚举 json 节点(如果使用 System.Text.Json,
JsonElement
),并通过根据当时发现自己的节点级别创建TreeViewItems
将它们添加到 WPF TreeView
中。
SO is not a place to quickly answer this question. SO不是快速回答这个问题的地方。
I have written a basic WPF JSON Treeview as a .Net Core WPF control that can be installed as a Nuget Package.我编写了一个基本的 WPF JSON Treeview 作为 .Net Core WPF 控件,可以作为 Nuget 包安装。 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.如果没有,请查看我的 Github 存储库,看看是否有一个示例可供您作为工作的基础。 ( Do you have an update to the code to suggest, please join the project and put in a PR ).
(你有更新的代码建议,请加入项目并提交 PR )。
TreeViewExtension.ProcessJson
to see how the tree and the package then work together to create a tree.TreeViewExtension.ProcessJson
以了解树和包如何协同工作以创建树。JsonExtensions.cs
to see how start to enumerate the tree to create items.JsonExtensions.cs
以了解如何开始枚举树以创建项目。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.