如何使用自定义树结构填充树视图? 我正在尝试做的事情:
我有一个Actor类,其中包含一个Actor数组(作为子级,每个子级都包含另一个子级数组,依此类推...),并且有一个Actor作为父级。我只是想以树状视图显示它们。演员类看起来像这样:
class Actor
{
public List<Actor> Children {get; set;} = new List<Actor>();
public Actor Parent {get; set;}
public Actor[] GetChildren()
{
return Children.ToArray();
}
}
private void UpdateSceneryTree()
{
SceneryStructureView.Nodes.Clear();
var root = new TreeNode(KerboEngine.CurrentScenery.Name) ImageIndex = 1, SelectedImageIndex = 1 };
ListScenery(root);
SceneryStructureView.Nodes.Add(root);
}
private void ListScenery(TreeNode root)
{
foreach (var sco in KerboEngine.CurrentScenery.RootStructures) //root structures are all roots which have no parent.
{
TreeNode n = new TreeNode(sco.Name) { Tag = sco };
List<TreeNode> ns = new List<TreeNode>();
foreach (var a in sco.GetChilds()) //here we get all childs of them
ns.Add(new TreeNode(a.Name) { Tag = a });
n.Nodes.AddRange(ns.ToArray());
root.Nodes.Add(n);
}
}
此代码有效,但仅在两个深度上起作用:
就像您看到“构造2”的Child1应该有2个孩子一样,我可以在属性网格中看到它们,但在树状视图中看不到。
有人知道无限制深度获取和列出树视图的解决方案吗?