zoukankan      html  css  js  c++  java
  • 星空雅梦

    WPF下递归生成树形数据绑定到TreeView上

     

    最终效果图:(用于学习类的效果 图片丑了点,看官莫怪)

    新建窗体 然后在前端适当位置插入如下代码:

    复制代码
      <TreeView x:Name="departmentTree"  Height="500" Width="500">
                                <TreeView.ItemTemplate>
                                    <HierarchicalDataTemplate ItemsSource="{Binding Nodes}">
                                        <StackPanel>
                                            <TextBlock VerticalAlignment="Center" FontSize="14" Text="{Binding deptName}" Margin="2,0,0,0"></TextBlock>
                                        </StackPanel>
                                    </HierarchicalDataTemplate>
                                </TreeView.ItemTemplate>
                            </TreeView>
    复制代码

    后端代码:

    复制代码
      public class DepartmentModel
        {
           public List<DepartmentModel> Nodes { get; set; }
           public DepartmentModel()
            {
                this.Nodes = new List<DepartmentModel>();
                this.ParentId = 0;//主节点的父id默认为0
            }
           public int id { get; set; }//id
           public string deptName { get; set; }//部门名称
           public int ParentId { get; set; }//父类id
        }
    复制代码
    复制代码
    public partial class TestWin : Window
        {
            public TestWin()
            {
                InitializeComponent();
                this.departmentTree.ItemsSource = getTrees(0, getDepts());//数据绑定
            }
    
    
            /// <summary>
            /// 获取部门列表--测试
            /// </summary>
            /// <returns></returns>
            public List<DepartmentModel> getDepts() {
                List<DepartmentModel> dplst = new List<DepartmentModel>(){
                new DepartmentModel(){id=1,deptName="主部门1",ParentId=0},
                new DepartmentModel(){id=2,deptName="主部门2",ParentId=0},
                new DepartmentModel(){id=3,deptName="主部门1_1",ParentId=1},
                new DepartmentModel(){id=4,deptName="主部门1_2",ParentId=1},
                new DepartmentModel(){id=5,deptName="主部门1_3",ParentId=1},
                new DepartmentModel(){id=6,deptName="主部门1_4",ParentId=1},
                new DepartmentModel(){id=7,deptName="主部门1_5",ParentId=1},
                new DepartmentModel(){id=8,deptName="主部门2_1",ParentId=2},
                new DepartmentModel(){id=9,deptName="主部门2_2",ParentId=2},
                new DepartmentModel(){id=10,deptName="主部门2_3",ParentId=2},
                new DepartmentModel(){id=11,deptName="主部门2_4",ParentId=2},
                new DepartmentModel(){id=12,deptName="主部门1_1_1",ParentId=3},
                new DepartmentModel(){id=13,deptName="主部门1_1_2",ParentId=3},
                new DepartmentModel(){id=14,deptName="主部门1_2_1",ParentId=4},
                new DepartmentModel(){id=15,deptName="主部门1_1_1_1",ParentId=12}
                };
                return dplst;
            }
    
            /// <summary>
            /// 递归生成树形数据
            /// </summary>
            /// <param name="delst"></param>
            /// <returns></returns>
            public List<DepartmentModel> getTrees(int parentid, 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 dpt in mainNodes)
                {
                    dpt.Nodes = getTrees(dpt.id,otherNodes);
                }
                return mainNodes;
            }
    
             
      
        }
    复制代码

    上述主要代码为【递归生成树形数据】方法

  • 相关阅读:
    hdu 2203 亲和串
    hdu 3999 The order of a Tree
    poj 2299 Ultra-QuickSort
    hdu 1908 Double Queue
    hdu 1556 Color the ball
    hdu 4288 Coder
    hdu 5265 pog loves szh II
    hdu 5058 So easy
    T103763 【模板】矩阵乘法
    T103492 【模板】点双连通分量
  • 原文地址:https://www.cnblogs.com/LiZhongZhongY/p/10898450.html
Copyright © 2011-2022 走看看