1. The first effect
Add all nodes to the ComboBox data source, and the following TreeView displays the child nodes under the node when selected in the ComboBox.
1.xaml file, add the following code to the appropriate location of the interface
1 <StackPanel>2 <StackPanelMargin= "Ten">3 <LabelContent= "Select group node:"></Label>4 <ComboBoxMaxdropdownheight= "+"Name= "Cmbgoup"dropdownclosed= "cmbgoup_dropdownclosed"></ComboBox>5 </StackPanel>6 <StackPanelMargin= "Ten">7 <TreeViewx:name= "Tvgroup">8 <treeview.itemtemplate>9 <hierarchicaldatatemplateItemsSource="{Binding Nodes}">Ten <StackPanel> One <TextBlockVerticalAlignment= "Center"FontSize= "+"Text="{Binding GroupName}"Margin= "2,0,0,0"></TextBlock> A </StackPanel> - </hierarchicaldatatemplate> - </treeview.itemtemplate> the </TreeView> - </StackPanel> - </StackPanel>
2. Background code
A. Node class for binding
1 Public classGroup2 {3 PublicGroup ()4 {5 This. Nodes =NewList<group>();6 This. ParentID =0;//The parent ID of the master node defaults to 07 }8 9 PublicList<group> Nodes {Get;Set; }Ten Public intID {Get;Set; }//ID One Public intParentID {Get;Set; }//ParentID A Public stringGroupName {Get;Set; } -}
B. Main interface Class Code
Public Partial classMainwindow:window { PublicMainWindow () {InitializeComponent (); #regionData for bindingList<Group> Grplst =NewList<group>(){ NewGroup () {id=0, groupname="Group", ParentID =-1}, NewGroup () {id=1, groupname="Group1", parentid=0}, NewGroup () {id=2, groupname="Group2", parentid=0}, NewGroup () {id=3, groupname="group1_1", parentid=1}, NewGroup () {id=4, groupname="group1_2", parentid=1}, NewGroup () {id=5, groupname="group1_3", parentid=1}, NewGroup () {id=6, groupname="group1_4", parentid=1}, NewGroup () {id=7, groupname="group1_5", parentid=1}, NewGroup () {id=8, groupname="Group2_1", parentid=2}, NewGroup () {id=9, groupname="group2_2", parentid=2}, NewGroup () {id=Ten, groupname="Group2_3", parentid=2}, NewGroup () {id= One, groupname="Group2_4", parentid=2}, NewGroup () {id= A, groupname="group1_1_1", parentid=3}, NewGroup () {id= -, groupname="group1_1_2", parentid=3}, NewGroup () {id= -, groupname="Group1_2_1", parentid=4}, NewGroup () {id= the, groupname="group1_1_1_1", parentid= A} }; #endregion This. Cmbgoup.itemssource = Grplst;//ComboBox Data Source This. Cmbgoup.selectedvaluepath ="ID"; This. Cmbgoup.displaymemberpath ="GroupName"; List<Group> Lstgroup = Gettreedata (-1, Grplst);//gets the data of the parent node-1 when initializing This. Tvgroup.itemssource = Lstgroup;//Data Binding } /// <summary> ///recursive generation of tree-shaped data/// </summary> /// <param name= "Delst" ></param> /// <returns></returns> PublicList<group> Gettreedata (intParentID, list<group>nodes) {List<Group> mainnodes = nodes. Where (x = X.parentid = = ParentID). Tolist<group>(); List<Group> othernodes = nodes. Where (x = x.parentid! = parentid). Tolist<group>(); foreach(Group GRPinchmainnodes) {grp. Nodes=Gettreedata (grp.id, othernodes); } returnMainnodes; } /// <summary> ///Drop-down box Close event/// </summary> /// <param name= "Sender" ></param> /// <param name= "E" ></param> Private voidCmbgoup_dropdownclosed (Objectsender, EventArgs e) { if( This. Cmbgoup.selectedvalue = =NULL) { return; } intGroupId = (int) This. Cmbgoup.selectedvalue;//the selected group numberlist<group> Lstgroup = Gettreedata (GroupId, (list<group>) Cmbgoup.itemssource); This. Tvgroup.itemssource =Lstgroup; } }
WPF TreeView Data Binding Recursive binding node