Reputation: 1
So I have a TreeView setup with a few nodes in it. I have a string list with a few strings in it, and I want to add the entire list into a child of a specific node in the TreeView using C# code (possibly a foreach loop?). How might I do that?
Upvotes: 0
Views: 3393
Reputation: 52107
Are you referring to the WPF TreeView or WinForms TreeView?
For WPF, the approach that worked best for me so far is to create a viewmodel and bind it to TreeView using HierarchicalDataTemplate. Basic idea is explained at http://www.codeproject.com/KB/WPF/TreeViewWithViewModel.aspx.
WinForms TreeView can be manipulated directly through TreeView.Nodes, TreeNode.Nodes and such. I recommend against trying to do something similar with WPF TreeView...
Upvotes: 1
Reputation: 1891
This will add three nodes to the child node "node00" of node "node0"
List<string> strings = new List<string>() { "string1", "string2", "string3" };
foreach (string s in strings)
treeView1.Nodes["node0"].Nodes["node00"].Nodes.Add(s, s);
Note that you can specify the nodes by name (key) or by index
Upvotes: 2