private void AddLeaf(TreeViewItem twItem, LeafNode node) { if (node == null) { return; } twItem.Header = node.Data; if (node.Children != null && node.Children.Count > 0) { for (int i = 0; i < node.Children.Count; i++) { TreeViewItem it = new TreeViewItem(); AddLeaf(it, node.Children[i]); twItem.Items.Add(it); } } }
private void InitTree() { for (int i = 0; i < 5; i++) { LeafNode child = new LeafNode() { Data = "root" + i.ToString() }; child.Children = new List <LeafNode>(); roots.Add(child); ////10 children for (int j = 0; j < 10; j++) { LeafNode subChild = new LeafNode() { Data = "son" + j.ToString() }; subChild.Children = new List <LeafNode>(); ////15 children child.Children.Add(subChild); for (int k = 0; k < 15; k++) { LeafNode gs = new LeafNode() { Data = "grantSon" + k.ToString() }; subChild.Children.Add(gs); } } } for (int i = 0; i < roots.Count; i++) { TreeViewItem item = new TreeViewItem(); AddLeaf(item, roots[i]); twLeaf.Items.Add(item); } }