简介:医院有很多工作单位,"" --->医院 code 是2位(如 10,20等)代表是医院的子节点,code是4位(如 1012,1011)是 10的子节点,以此类推;用递归的方法用这些数据构建TreeView树。 效果图如下:
代码如下:
构建TreeView的xaml < Window x:Class = " CreateTree.MainWindow " xmlns = " http://schemas.microsoft.com/winfx/2006/xaml/presentation " xmlns:x = " http://schemas.microsoft.com/winfx/2006/xaml " Title = " MainWindow " Height = " 429 " Width = " 331 " > < Grid Height = " 391 " > < TreeView Height = " 274 " HorizontalAlignment = " Left " Margin = " 12,12,0,0 " Name = " tvOrg " VerticalAlignment = " Top " Width = " 174 " /> </ Grid > </ Window >构建TreeView cs文件 namespace CreateTree{ /// <summary> /// MainWindow.xaml 的交互逻辑 /// </summary> public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); // 根结点 TreeOrgDict treeDict = new TreeOrgDict(); treeDict.OrgCode = "" ; treeDict.OrgName = " 医院单位 " ; List < OrgDict > orgList = GetOrgList(); // 递归多次调用方法GetNodeList,所以要放在方法的外面作为参数 // 获取根结点的子节点 treeDict.SubList = GetNodeList( "" , orgList, 0 ); TreeViewItem newItem = new TreeViewItem() { Tag = treeDict, Header = treeDict.OrgName }; newItem.IsExpanded = true ; if (treeDict.SubList != null ) { // 递归赋值 SetTreeNodes(treeDict.SubList.ToList(), newItem); } this .tvOrg.Items.Add(newItem); } // 递归算法组装树 public List < TreeOrgDict > GetNodeList( string orgcode, List < OrgDict > orgList, int num) { int loop = num + 2 ; // 子结点code的位数 List < OrgDict > rootNoods = new List < OrgDict > (); List < TreeOrgDict > reList = new List < TreeOrgDict > (); if (orgcode == "" ) { // 根结点 rootNoods = (from org in orgList where org.OrgCode.Length == 2 select org).ToList(); } else { // 非根结点 rootNoods = (from org in orgList where org.OrgCode.Length == loop && org.OrgCode.Substring( 0 , num).Equals(orgcode) select org).ToList(); } // 递归出口 if (rootNoods.Count() == 0 ) { return null ; } else { foreach (var node in rootNoods) { TreeOrgDict treedict = new TreeOrgDict(); treedict.OrgCode = node.OrgCode; treedict.OrgName = node.OrgName; treedict.SubList = GetNodeList(node.OrgCode, orgList, loop); reList.Add(treedict); } } return reList; } // 递归给树赋值 public void SetTreeNodes(List < TreeOrgDict > treeSubList, TreeViewItem newItem) { foreach (var node in treeSubList) { TreeViewItem subItem = new TreeViewItem() { Tag = node, Header = node.OrgName }; if (node.SubList != null ) { SetTreeNodes(node.SubList.ToList(), subItem); } newItem.Items.Add(subItem); } } // 获取单位的列表 public List < OrgDict > GetOrgList() { List < OrgDict > orgList = new List < OrgDict > (); OrgDict org10 = new OrgDict( " 10 " , " 政治部 " ); OrgDict org1001 = new OrgDict( " 1001 " , " 政治部办公室 " ); OrgDict org1002 = new OrgDict( " 1002 " , " 组织处 " ); OrgDict org1003 = new OrgDict( " 1003 " , " 干部处 " ); OrgDict org11 = new OrgDict( " 11 " , " 内科临床部 " ); OrgDict org1101 = new OrgDict( " 1101 " , " 内分泌科师干病室 " ); OrgDict org1102 = new OrgDict( " 1102 " , " 门诊内窥镜室 " ); OrgDict org16 = new OrgDict( " 16 " , " 临床部 " ); OrgDict org1601 = new OrgDict( " 1601 " , " 南楼二科 " ); OrgDict org1602 = new OrgDict( " 1602 " , " 南楼四科 " ); OrgDict org1603 = new OrgDict( " 1603 " , " 南楼六科 " ); OrgDict org20 = new OrgDict( " 20 " , " 院首长 " ); OrgDict org2010 = new OrgDict( " 2010 " , " 院长办公室 " ); OrgDict org2012 = new OrgDict( " 2012 " , " 副院长办公室 " ); OrgDict org2016 = new OrgDict( " 2016 " , " 党委书记办公室 " ); OrgDict org2018 = new OrgDict( " 2018 " , " 副党委书记办公室 " ); orgList.Add(org10); orgList.Add(org1001); orgList.Add(org1002); orgList.Add(org1003); orgList.Add(org11); orgList.Add(org1101); orgList.Add(org1102); orgList.Add(org16); orgList.Add(org1601); orgList.Add(org1602); orgList.Add(org1603); orgList.Add(org20); orgList.Add(org2010); orgList.Add(org2012); orgList.Add(org2016); orgList.Add(org2018); return orgList; } } // 单位类 public class OrgDict { // 单位编号 private string orgcode; public string OrgCode { get { return orgcode; } set { orgcode = value; } } // 单位名称 private string orgname; public string OrgName { get { return orgname; } set { orgname = value; } } // 构造方法 public OrgDict( string code, string name) { this .orgcode = code; this .orgname = name; } } // 单位结点类 public class TreeOrgDict { // 单位编号 private string orgcode; public string OrgCode { get { return orgcode; } set { orgcode = value; } } // 单位名称 private string orgname; public string OrgName { get { return orgname; } set { orgname = value; } } // 字节点列表 private List < TreeOrgDict > subList; public List < TreeOrgDict > SubList { get { return subList; } set { subList = value; } } }}
转载于:https://www.cnblogs.com/shuming/archive/2011/01/20/1940628.html
