把潜在的树形结构数据转换成树形结构数据

实体类:

 1 using System.Collections.Generic;
 2 using System.ComponentModel;
 3
 4 namespace TreeViewBindingDemo
 5 {
 6     public class NodeEntry : INotifyPropertyChanged
 7     {
 8         public NodeEntry()
 9         {
10             this.NodeEntrys = new List<NodeEntry>();
11             this.ParentID = -1;
12             this.IsChecked = true;
13         }
14         int id;
15         public int ID
16         {
17             get { return id; }
18             set { id = value; this.OnPropertyChanged("ID"); }
19         }
20         string name;
21         public string Name
22         {
23             get { return name; }
24             set { name = value; this.OnPropertyChanged("Name"); }
25         }
26         public int ParentID { get; set; }
27         bool isChecked;
28         public bool IsChecked
29         {
30             get { return isChecked; }
31             set { isChecked = value; this.OnPropertyChanged("IsChecked"); }
32         }
33         List<NodeEntry> nodeEntrys;
34         public List<NodeEntry> NodeEntrys
35         {
36             get { return nodeEntrys; }
37             set
38             {
39                 nodeEntrys = value;
40                 this.OnPropertyChanged("NodeEntrys");
41             }
42         }
43         public event PropertyChangedEventHandler PropertyChanged;
44         private void OnPropertyChanged(string prop)
45         {
46             if (this.PropertyChanged != null)
47                 this.PropertyChanged(this, new PropertyChangedEventArgs(prop));
48         }
49     }
50
51 }

转换代码:

  1 using System;
  2 using System.Collections.Generic;
  3 using System.Linq;
  4 using System.Text;
  5 using System.Threading.Tasks;
  6 using System.Windows;
  7 using System.Windows.Controls;
  8 using System.Windows.Data;
  9 using System.Windows.Documents;
 10 using System.Windows.Input;
 11 using System.Windows.Media;
 12 using System.Windows.Media.Imaging;
 13 using System.Windows.Navigation;
 14 using System.Windows.Shapes;
 15
 16 namespace TreeViewBindingDemo
 17 {
 18     /// <summary>
 19     /// MainWindow.xaml 的交互逻辑
 20     /// </summary>
 21     public partial class MainWindow : Window
 22     {
 23         List<NodeEntry> m_NodeEntrys;
 24         List<NodeEntry> m_outputList;
 25         public MainWindow()
 26         {
 27             InitializeComponent();
 28             m_NodeEntrys = new List<NodeEntry>()
 29         {
 30             new NodeEntry { ID = 2, Name = "北京市", ParentID = 1 },
 31             new NodeEntry { ID = 1, Name = "中国" },
 32             new NodeEntry { ID = 3, Name = "吉林省", ParentID = 1 },
 33             new NodeEntry { ID = 4, Name = "上海市", ParentID = 1 },
 34             new NodeEntry { ID = 5, Name = "海淀区", ParentID = 2 },
 35             new NodeEntry { ID = 6, Name = "朝阳区", ParentID = 2 },
 36             new NodeEntry { ID = 7, Name = "大兴区", ParentID = 2 },
 37             new NodeEntry { ID = 8, Name = "白山市", ParentID = 3 },
 38             new NodeEntry { ID = 9, Name = "长春市", ParentID = 3 },
 39             new NodeEntry { ID = 10, Name = "抚松县", ParentID = 8 },
 40             new NodeEntry { ID = 11, Name = "靖宇县", ParentID = 8 },
 41             new NodeEntry { ID = 13, Name = "靖宇县" },
 42             new NodeEntry { ID = 14, Name = "无名村", ParentID = 13 },
 43         };
 44             m_outputList = Bind(m_NodeEntrys);
 45             this.treeView1.ItemsSource = m_outputList;
 46             this.treeView2.ItemsSource = m_outputList;
 47         }
 48
 49         private List<NodeEntry> Bind(List<NodeEntry> nodes)
 50         {
 51             List<NodeEntry> outputList=new List<NodeEntry>();
 52             for (int i = 0; i < nodes.Count; i++)
 53             {
 54                 nodes[i].IsChecked = false;
 55                 if (nodes[i].ParentID == -1)
 56                 {
 57                     outputList.Add(nodes[i]);
 58                 }
 59                 else
 60                 {
 61                     FindDownward(nodes,nodes[i].ParentID).NodeEntrys.Add(nodes[i]);
 62                 }
 63             }
 64             return outputList;
 65         }
 66
 67         private NodeEntry FindDownward(List<NodeEntry> nodes, int id)
 68         {
 69             if (nodes == null) return null;
 70             for (int i = 0; i < nodes.Count; i++)
 71             {
 72                 if (nodes[i].ID == id)
 73                 {
 74                     return nodes[i];
 75                 }
 76             }
 77             return null;
 78         }
 79
 80         private void btnOK_Click(object sender, RoutedEventArgs e)
 81         {
 82             try
 83             {
 84                 m_NodeEntrys.Add(new NodeEntry { ID = 14, IsChecked = true, Name = "法国" });
 85                 m_outputList.Add(new NodeEntry { ID = 14, IsChecked = true, Name = "法国" });
 86                 //m_outputList = Bind(m_NodeEntrys);
 87                 NodeEntry node = new NodeEntry();
 88                 this.treeView1.ItemsSource = m_outputList;
 89                 this.treeView2.ItemsSource = null;
 90                 this.treeView2.ItemsSource = m_outputList;
 91             }
 92             catch (Exception ex)
 93             {
 94             }
 95         }
 96         private void btnCancel_Click(object sender, RoutedEventArgs e)
 97         {
 98
 99         }
100
101         //双向绑定改名,选择
102         private void treeView2_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
103         {
104             NodeEntry item = (NodeEntry)this.treeView2.SelectedItem;
105             item.Name = "dido";
106             item.IsChecked = true;
107             MessageBox.Show(item.ID.ToString());
108         }
109
110
111     }
112 }

时间: 2024-08-08 21:53:35

把潜在的树形结构数据转换成树形结构数据的相关文章

c# List列表数据转换成树形结构

把List列表结构 转换成树形结构 /// <summary> /// 构造树形Json /// </summary> public static class TreeJson { /// <summary> /// 转换树Json /// </summary> /// <param name="list">数据源</param> /// <param name="parentId">

mktime(将时间结构数据转换成经过的秒数)

mktime(将时间结构数据转换成经过的秒数)表头文件#include<time.h>定义函数time_t mktime(strcut tm * timeptr);函数说明mktime()用来将参数timeptr所指的tm结构数据转换成从公元1970年1月1日0时0分0 秒算起至今的UTC时间所经过的秒数.返回值返回经过的秒数.范例/* 用time()取得时间(秒数),利用localtime()转换成struct tm 再利用mktine()将struct tm转换成原来的秒数*/#inclu

js将有父子关系的数据转换成树形结构数据

比如如下基本数据: let allDatas = [ { id: 3, name: 'bbbb', parendId: 1 }, { id: 2, name: 'aaaaa', parendId: 2 }, { id: 4, name: 'ccccc', parendId: 1 }, { id: 5, name: 'ddddd', parendId: 4 }, { id: 6, name: 'eeeee', parendId: 4 }, { id: 7, name: 'ffff', parend

递归生成树形结构

原文地址:https://blog.csdn.net/q13965211/article/details/80772544 节点树树形结构 Tree 结构 import java.util.List; /** * @Author fanwei * @date 2018-6-13 17:04 */ public class TreeNode { private Integer key; private String title; private Integer parentId; private

级联数据生成树形结构

原文地址:https://www.cnblogs.com/newlangwen/p/9969904.html Tree 结构 public class TreeNode { private Integer key; private String title; private Integer parentId; private List<TreeNode> children; public Integer getKey() { return key; } public String getTit

递归算法解析成树形结构

/** * 递归算法解析成树形结构 * * @param cid * @return * @author jiqinlin */ public TreeNodeModel recursiveTree(int org_code) { //根据cid获取节点对象(SELECT * FROM tb_tree t WHERE t.cid=?) MiddleOrgEntity middleOrgEntity = new MiddleOrgEntity(); middleOrgEntity.setTable

10,组合模式(Composite Pattern)是将对象组合成树形结构以表示“部分--整体”的层次结构。使得用户对单个对象和组合对象的使用具有一致性。

Composite模式也叫组合模式,是构造型的设计模式之一.通过递归手段来构造树形的对象结构,并可以通过一个对象来访问整个对象树. Component (树形结构的节点抽象) - 为所有的对象定义统一的接口(公共属性,行为等的定义) - 提供管理子节点对象的接口方法 - [可选]提供管理父节点对象的接口方法  Leaf (树形结构的叶节点) Component的实现子类  Composite(树形结构的枝节点) Component的实现子类 适用于: 单个对象和组合对象的使用具有一致性.将对象组

使用C#中的DirectorySearcher来获得活动目录中的组织结构与用户等信息,并在展示成树形结构(附源代码)

使用C#中的DirectorySearcher来获得活动目录中的组织结构与用户等信息,并在展示成树形结构(附源代码) 对于C#来说,取得活动目录中的组织结构相对简单,因为其在System.DirectoryServices命名空间中内置了DirectorySearcher的方法,我们可以组合多种过滤方式,来达到取得活动目录中的所有信息,当然,我现在还没有找到可以得到域用户密码的方式 :) 以下是关键片段 1private static SearchResultCollection _ADHelp

记一则 Lambda内递归调用方法将集合对象转换成树形结构

public dynamic GetDepartments(string labID) { List<int> usedIDs = new List<int>(); //缓存已用过的ID //定义递归算法 Func<object,List<DepartmentItem>, List<DepartmentItem>, dynamic> recursion = (r,d,a) => { List<dynamic> dyData =