XAML文件
<Window x:Class="WpfTreeviewDemo.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mode="clr-namespace:WpfTreeviewDemo"
Title="TreeView无限级树绑定事例" Height="300" Width="300" WindowStartupLocation="CenterScreen" DataContext="{Binding}" ResizeMode="CanMinimize">
<Grid>
<Grid.Resources>
<HierarchicalDataTemplate DataType="{x:Type mode:Node}" ItemsSource="{Binding Nodes}">
<StackPanel Orientation="Horizontal" Margin="0,2,0,2"><TextBlock Text="{Binding Name}" ToolTip="{Binding Name}" Tag="{Binding}"/>
</StackPanel>
</HierarchicalDataTemplate>
</Grid.Resources>
<TreeView Name="TreeView"/>
</Grid>
</Window>
C#代码文件
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using System.Windows;
6 using System.Windows.Controls;
7 using System.Windows.Data;
8 using System.Windows.Documents;
9 using System.Windows.Input;
10 using System.Windows.Media;
11 using System.Windows.Media.Imaging;
12 using System.Windows.Navigation;
13 using System.Windows.Shapes;
14 using System.Threading;
15 using System.Diagnostics;
16 using System.Windows.Markup;
17
18
19 namespace WpfTreeviewDemo
20 {
21 //定义节点类Node
22 public class Node
23 {
24 //构造函数
25 public Node()
26 {
27 this.Nodes = new List<Node>();
28 }
29 public int ID { get; set; }//内码
30 public string Name { get; set; }//名称
31 public bool IsParent { get; set; }//是否父级
32 public int OwnerID { get; set; }//上一级内码
33 public List<Node> Nodes { get; set; }//节点集合
34 }
35
36 /// <summary>
37 /// MainWindow.xaml 的交互逻辑
38 /// </summary>
39 public partial class MainWindow : Window
40 {
41 public MainWindow()
42 {
43 InitializeComponent();
44 List<Node> nodesList = new List<Node>()//创建节点的数据集合对象
45 {
46 new Node { ID = 1000, Name = "系统" , IsParent=true, OwnerID = 0 },
47 new Node { ID = 1100, Name = "软件设置", IsParent=false, OwnerID = 1000 },
48 new Node { ID = 1200, Name = "数据备份", IsParent=false, OwnerID = 1000 },
49 new Node { ID = 1300, Name = "数据恢复", IsParent=false, OwnerID = 1000 },
50 new Node { ID = 2000, Name = "基础资料", IsParent=true, OwnerID = 0 },
51 new Node { ID = 2100, Name = "用户管理", IsParent=false, OwnerID = 2000 },
52 new Node { ID = 2200, Name = "用户组 ", IsParent=false, OwnerID = 2000 },
53 new Node { ID = 2300, Name = "组织机构", IsParent=false, OwnerID = 2000 },
54 };
55 List<Node> outputList = Bind(nodesList);// 绑定树
56 this.TreeView.ItemsSource = outputList;//绑定TreeView.Items数据源
57 }
58
59 /// <summary>
60 /// 绑定树
61 /// </summary>
62 List<Node> Bind(List<Node> nodes)
63 {
64 List<Node> outputList = new List<Node>();//定义节点集合outputList
65 for (int i = 0; i < nodes.Count; i++)//按节点数量循环
66 {
67 if (nodes[i].OwnerID == 0)//判断是否根节点
68 {
69 outputList.Add(nodes[i]);//是根节点的,节点集合中加上节点
70 }
71 else
72 {
73 FindDownward(nodes, nodes[i].OwnerID).Nodes.Add(nodes[i]);//不是根节点的向下查找
74 }
75 }
76 return outputList;
77 }
78
79 /// <summary>
80 /// 向下查找
81 /// </summary>
82 Node FindDownward(List<Node> nodes, int id)
83 {
84 if (nodes == null)
85 {
86 return null;//节点集合为空返回null
87 }
88 for (int i = 0; i < nodes.Count; i++)//按nodes节点集合数量循环
89 {
90 if (nodes[i].ID == id)//判断节点的id是否与父节点相同
91 {
92 return nodes[i];//是父节点返回节点
93 }
94 Node node = FindDownward(nodes[i].Nodes, id);//向下查找
95 if (node != null)//节点不等于返回节点
96 {
97 return node;
98 }
99 }
100 return null;//返回空值
101 }
102 }
103 }