WPF中TreeView的items一般為TreeViewItem,同時TreeViewItem自身又可以包含新的TreeViewItem,從而形成一棵子樹,你想要幾級節點就有幾級節點了。C#如下: TreeView treeView = new TreeView(); //第一級,在treeView上新增 TreeViewItem treeViewItem1 = new TreeViewItem(); treeViewItem1.Header = "第一級"; treeView.Items.Add(treeViewItem1); //第二級,在treeViewItem1上新增 TreeViewItem treeViewItem2 = new TreeViewItem(); treeViewItem2.Header = "第二級"; treeViewItem1.Items.Add(treeViewItem2); //第三級,在treeViewItem2上新增 TreeViewItem treeViewItem3 = new TreeViewItem(); treeViewItem3.Header = "第三級"; treeViewItem2.Items.Add(treeViewItem3);結果圖:
WPF中TreeView的items一般為TreeViewItem,同時TreeViewItem自身又可以包含新的TreeViewItem,從而形成一棵子樹,你想要幾級節點就有幾級節點了。C#如下: TreeView treeView = new TreeView(); //第一級,在treeView上新增 TreeViewItem treeViewItem1 = new TreeViewItem(); treeViewItem1.Header = "第一級"; treeView.Items.Add(treeViewItem1); //第二級,在treeViewItem1上新增 TreeViewItem treeViewItem2 = new TreeViewItem(); treeViewItem2.Header = "第二級"; treeViewItem1.Items.Add(treeViewItem2); //第三級,在treeViewItem2上新增 TreeViewItem treeViewItem3 = new TreeViewItem(); treeViewItem3.Header = "第三級"; treeViewItem2.Items.Add(treeViewItem3);結果圖: