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;
        }

         
  
    }

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

本人第一次写技术博客 

望各位高手不吝赐教

posted @ 2016-04-19 17:06  魂断蓝桥666  阅读(15509)  评论(4编辑  收藏  举报