这是winform的递归,可以参考一下:
TreeViewList.Nodes.Clear(); foreach (var item in lste) { if (item.ParentID == RootID) { if (TreeViewList.Nodes.ContainsKey(item.ID)) { continue; } TreeNode tNode = new TreeNode(); tNode = TreeViewList.Nodes.Add(item.ID, item.Name); tNode.Tag = item.ID; tNode.ContextMenuStrip = Contextmenustrip; lstInfo = lste.Where(p => p.ParentID == item.ID).ToList(); AddDataToTreeView(lstInfo, tNode, 0); } } private void AddDataToTreeView(List<TviewListInfo> iEnumerable, TreeNode t, int level) { foreach (TviewListInfo c in iEnumerable) { if (TreeViewList.Nodes.ContainsKey(c.ID)) { continue; } TreeNode treenode = t.Nodes.Add(c.ID, c.Name); treenode.ContextMenuStrip = Contextmenustrip; treenode.Tag = c.ID; TreeViewList.Update(); lstInfo = lst.Where(p => p.ParentID == c.ID).ToList(); AddDataToTreeView(lstInfo, treenode, level + 1); } }
这个是WPF的,非常的全面:
http://www.codeproject.com/Articles/124644/Basic-Understanding-of-Tree-View-in-WPF
牛人