Final: (for learning the effects of the class picture is a bit ugly, crossing mo Strange)
Create a new form and insert the following code in the appropriate location on the front:
<treeview x:name="Departmenttree"height=" -"Width=" -"> <TreeView.ItemTemplate> "{Binding Nodes}"> <StackPanel> <textblock Verticalal Ignment="Center"Fontsize=" -"text="{Binding Deptname}"margin="2,0,0,0"></TextBlock> </StackPanel> Back-End Code:
Public classDepartmentmodel { PublicList<departmentmodel> Nodes {Get;Set; } PublicDepartmentmodel () { This. Nodes =NewList<departmentmodel>(); This. ParentID =0;//The parent ID of the master node defaults to 0 } Public intID {Get;Set; }//ID Public stringDeptname {Get;Set; }//Department Name Public intParentID {Get;Set; }//Parent class ID}
Public Partial classTestwin:window { PublicTestwin () {InitializeComponent (); This. Departmenttree.itemssource = Gettrees (0, getdepts ());//Data Binding } /// <summary> ///Get Department List--Test/// </summary> /// <returns></returns> PublicList<departmentmodel>getdepts () {List<DepartmentModel> Dplst =NewList<departmentmodel>(){ NewDepartmentmodel () {id=1, deptname="Main Department 1", parentid=0}, NewDepartmentmodel () {id=2, deptname="Main Department 2", parentid=0}, NewDepartmentmodel () {id=3, deptname="Main Department 1_1", parentid=1}, NewDepartmentmodel () {id=4, deptname="Main Department 1_2", parentid=1}, NewDepartmentmodel () {id=5, deptname="Main Department 1_3", parentid=1}, NewDepartmentmodel () {id=6, deptname="Main Department 1_4", parentid=1}, NewDepartmentmodel () {id=7, deptname="Main Department 1_5", parentid=1}, NewDepartmentmodel () {id=8, deptname="Main Department 2_1", parentid=2}, NewDepartmentmodel () {id=9, deptname="Main Department 2_2", parentid=2}, NewDepartmentmodel () {id=Ten, deptname="Main Department 2_3", parentid=2}, NewDepartmentmodel () {id= One, deptname="Main Department 2_4", parentid=2}, NewDepartmentmodel () {id= A, deptname="Main Department 1_1_1", parentid=3}, NewDepartmentmodel () {id= -, deptname="Main Department 1_1_2", parentid=3}, NewDepartmentmodel () {id= -, deptname="Main Department 1_2_1", parentid=4}, NewDepartmentmodel () {id= the, deptname="Main Department 1_1_1_1", parentid= A} }; returnDplst; } /// <summary> ///recursive generation of tree-shaped data/// </summary> /// <param name= "Delst" ></param> /// <returns></returns> PublicList<departmentmodel> Gettrees (intParentID, list<departmentmodel>nodes) {List<DepartmentModel> mainnodes = nodes. Where (x = X.parentid = = ParentID). Tolist<departmentmodel>(); List<DepartmentModel> othernodes = nodes. Where (x = x.parentid! = parentid). Tolist<departmentmodel>(); foreach(Departmentmodel DPTinchmainnodes) {DPT. Nodes=gettrees (dpt.id,othernodes); } returnMainnodes; } }
The main code above is "recursive spanning tree data" method
The first time I wrote a technical blog
Hope that the master of the liberal enlighten you
WPF recursive spanning tree data binding to the TreeView