【问题标题】:How do I add node children to a TreeView using C# code?如何使用 C# 代码将节点子级添加到 TreeView?
【发布时间】:2011-07-30 00:05:01
【问题描述】:

所以我有一个包含几个节点的 TreeView 设置。我有一个包含几个字符串的字符串列表,我想使用 C# 代码(可能是 foreach 循环?)将整个列表添加到 TreeView 中特定节点的子节点中。我该怎么做?

【问题讨论】:

    标签: c# treeview


    【解决方案1】:

    这会将三个节点添加到节点“node0”的子节点“node00”

    List<string> strings = new List<string>() { "string1", "string2", "string3" };
    
    foreach (string s in strings)
        treeView1.Nodes["node0"].Nodes["node00"].Nodes.Add(s, s);
    

    请注意,您可以通过名称(键)或索引指定节点

    【讨论】:

      【解决方案2】:

      您指的是 WPF TreeView 还是 WinForms TreeView?

      对于 WPF,到目前为止最适合我的方法是创建一个视图模型并使用 HierarchicalDataTemplate 将其绑定到 TreeView。基本思想在http://www.codeproject.com/KB/WPF/TreeViewWithViewModel.aspx进行了解释。

      WinForms TreeView可以通过TreeView.Nodes、TreeNode.Nodes等直接操作。我建议不要尝试使用 WPF TreeView 做类似的事情...

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2013-07-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-04-16
        • 2020-08-25
        • 1970-01-01
        相关资源
        最近更新 更多