一, 新建了 WPF 项目作为测试, 使用 TreeView 控件进行界面展示.
第一步创建实体类 TreeEntity:
- public class TreeEntity
- {
- private int _mid;
- private string _title;
- private int _pid;
- private List<TreeEntity> _childrens=new List<TreeEntity>();
- /// <summary>
- /// 项 ID
- /// </summary>
- public int Mid
- {
- get { return _mid; }
- set
- {
- _mid = value;
- }
- }
- /// <summary>
- /// 项名称
- /// </summary>
- public string Title
- {
- get { return _title; }
- set
- {
- _title = value;
- }
- }
- /// <summary>
- /// 父亲 ID
- /// </summary>
- public int Pid
- {
- get { return _pid; }
- set
- {
- _pid = value;
- }
- }
- /// <summary>
- /// 儿子们
- /// </summary>
- public List<TreeEntity> Childrens
- {
- get { return _childrens; }
- set
- {
- _childrens = value;
- }
- }
- }
第二部构造无层级的数据源:
- TreeEntity yeye = new TreeEntity()
- {
- Mid = 10,
- Title = "猪爷爷",
- Pid = 0
- };
- TreeEntity laolao = new TreeEntity()
- {
- Mid = 16,
- Title = "猪姥姥",
- Pid = 0
- };
- TreeEntity baba = new TreeEntity()
- {
- Mid = 24,
- Title = "猪爸爸",
- Pid = 10
- };
- TreeEntity mama = new TreeEntity()
- {
- Mid = 25,
- Title = "猪妈妈",
- Pid = 16
- };
- TreeEntity peiqi = new TreeEntity()
- {
- Mid = 36,
- Title = "佩奇",
- Pid = 25
- };
- TreeEntity peizierzi = new TreeEntity()
- {
- Mid = 45,
- Title = "小佩佩奇",
- Pid = 36
- };
- TreeEntity peiqisunzi = new TreeEntity()
- {
- Mid = 55,
- Title = "佩奇的孙子",
- Pid = 45
- };
- TreeEntity peiqinver = new TreeEntity()
- {
- Mid = 42,
- Title = "小佩佩妞",
- Pid = 36
- };
- TreeEntity qiaozhi = new TreeEntity()
- {
- Mid = 38,
- Title = "乔治",
- Pid = 25
- };
- List<TreeEntity> treeEntities = new List<TreeEntity>()
- {
- yeye,
- laolao,
- mama,
- peiqi,
- peiqinver,
- peiqisunzi,
- qiaozhi,
- peizierzi
- };
第三步实现树形集合的构造
方法一: 使用递归方法构造
1), 先取出顶级数据项 -- 即无父亲的项
- List<TreeEntity> rootLists = treeEntities.Where(x => x.Pid == 0).ToList();
- for (int i = 0; i <rootLists.Count; i++)
- {
- rootLists[i].Childrens = CreateTree(rootLists[i].Mid, treeEntities);
- }
2), 再遍历上一步取得的数据项, 使用递归获取所有子项
- private List<TreeEntity> CreateTree(int pid, List<TreeEntity> sources)
- {
- List<TreeEntity> childrens = new List<TreeEntity>();
- if (sources != null && sources.Count> 0)
- {
- childrens = sources.Where(x => x.Pid == pid).ToList();
- if (childrens != null & childrens.Count> 0)
- {
- foreach (var item in childrens)
- {
- item.Childrens = CreateTree(item.Mid, sources);
- }
- }
- }
- return childrens;
- }
3), 将集合绑定给 TreeView 的数据源
MyTreeList.ItemsSource = rootLists;
方法二: 非递归遍历
1), 将所有数据项装进字典中 字典中的键为 当前实例的 ID; 字典中的项为当前实例
- Dictionary<int, TreeEntity> valuePairs = new Dictionary<int, TreeEntity>();
- foreach (var item in treeEntities)
- {
- valuePairs[item.Mid] = item;
- }
- List<TreeEntity> rootLists =CreateTreeQuick(valuePairs);
2), 遍历字典中的所有 Values, 向当前项 (item) 的 PID 对应的实例中添加当前项(item)
- private List<TreeEntity> CreateTreeQuick(Dictionary<int, TreeEntity> keyValues)
- {
- List<TreeEntity> myTree = new List<TreeEntity>();
- foreach (var item in keyValues.Values)
- {
- if (item.Pid==0)
- {
- myTree.Add(item);
- }
- else
- {
- if (keyValues.ContainsKey(item.Pid))
- {
- keyValues[item.Pid].Childrens.Add(item);
- }
- }
- }
- return myTree;
- }
3), 将集合绑定到 TreeView 控件的数据源中
MyTreeList.ItemsSource = rootLists;
二, WPF 前台布局
- <TreeView x:Name="MyTreeList" Width="200">
- <TreeView.ItemTemplate>
- <HierarchicalDataTemplate DataType="{x:Type local:TreeEntity}" ItemsSource="{Binding Childrens}">
- <TextBlock Text="{Binding Title}"/>
- </HierarchicalDataTemplate>
- </TreeView.ItemTemplate>
- </TreeView>
来源: http://www.bubuko.com/infodetail-3297714.html