WPF recursive spanning tree data binding to the TreeView

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.