zoukankan      html  css  js  c++  java
  • wpf treeview 数据绑定 递归绑定节点

    1.先上效果

    将所有节点加入ComboBox数据源,在ComboBox中选择时下方Treeview显示该节点下的子节点。

    1.xaml文件,将以下代码加入界面合适位置

     1     <StackPanel>
     2             <StackPanel Margin="10">
     3                 <Label Content="选择组节点:"></Label>
     4                 <ComboBox MaxDropDownHeight="100" Name="cmbGoup" DropDownClosed="cmbGoup_DropDownClosed"></ComboBox>
     5             </StackPanel>
     6             <StackPanel Margin ="10">
     7                 <TreeView x:Name="tvGroup">
     8                     <TreeView.ItemTemplate>
     9                         <HierarchicalDataTemplate ItemsSource="{Binding Nodes}">
    10                             <StackPanel>
    11                                 <TextBlock VerticalAlignment="Center" FontSize="14" Text="{Binding GroupName}" Margin="2,0,0,0"></TextBlock>
    12                             </StackPanel>
    13                         </HierarchicalDataTemplate>
    14                     </TreeView.ItemTemplate>
    15                 </TreeView>
    16             </StackPanel>
    17         </StackPanel>

    2.后台代码

    a.用于绑定的节点类

     1     public class Group
     2     {
     3         public Group()
     4         {
     5             this.Nodes = new List<Group>();
     6             this.ParentId = 0;//主节点的父id默认为0
     7         }
     8 
     9         public List<Group> Nodes { get; set; }
    10         public int ID { get; set; }//id
    11         public int ParentId { get; set; }//parentID
    12         public string GroupName { get; set; }
    13     }

    b.主界面类代码

    public partial class MainWindow : Window
        {
            public MainWindow()
            {
                InitializeComponent();
    
                #region 用于绑定的数据
                List<Group> grpLst = new List<Group>(){
                    new Group(){ID=0,GroupName="Group", ParentId = -1},
                    new Group(){ID=1,GroupName="Group1",ParentId=0},
                    new Group(){ID=2,GroupName="Group2",ParentId=0},
                    new Group(){ID=3,GroupName="Group1_1",ParentId=1},
                    new Group(){ID=4,GroupName="Group1_2",ParentId=1},
                    new Group(){ID=5,GroupName="Group1_3",ParentId=1},
                    new Group(){ID=6,GroupName="Group1_4",ParentId=1},
                    new Group(){ID=7,GroupName="Group1_5",ParentId=1},
                    new Group(){ID=8,GroupName="Group2_1",ParentId=2},
                    new Group(){ID=9,GroupName="Group2_2",ParentId=2},
                    new Group(){ID=10,GroupName="Group2_3",ParentId=2},
                    new Group(){ID=11,GroupName="Group2_4",ParentId=2},
                    new Group(){ID=12,GroupName="Group1_1_1",ParentId=3},
                    new Group(){ID=13,GroupName="Group1_1_2",ParentId=3},
                    new Group(){ID=14,GroupName="Group1_2_1",ParentId=4},
                    new Group(){ID=15,GroupName="Group1_1_1_1",ParentId=12}
                };
                #endregion
    
                this.cmbGoup.ItemsSource = grpLst;//comboBox数据源
                this.cmbGoup.SelectedValuePath = "ID";
                this.cmbGoup.DisplayMemberPath = "GroupName";
    
                List<Group> lstGroup = getTreeData(-1, grpLst);//初始化时获取父节点为-1的数据
                this.tvGroup.ItemsSource = lstGroup;//数据绑定
            }
    
            /// <summary>
            /// 递归生成树形数据
            /// </summary>
            /// <param name="delst"></param>
            /// <returns></returns>
            public List<Group> getTreeData(int parentid, 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 grp in mainNodes)
                {
                    grp.Nodes = getTreeData(grp.ID, otherNodes);
                }
                return mainNodes;
            }
    
            /// <summary>
            /// 下拉框关闭事件
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private void cmbGoup_DropDownClosed(object sender, EventArgs e)
            {
                if (this.cmbGoup.SelectedValue == null)
                {
                    return;
                }
                int groupId = (int)this.cmbGoup.SelectedValue;//选中的组号
                List<Group> lstGroup = getTreeData(groupId, (List<Group>)cmbGoup.ItemsSource);
                this.tvGroup.ItemsSource = lstGroup;
            }
        }
  • 相关阅读:
    Gerrit 系统初探 (已转移到 https://steemit.com/gerrit/@linvictor88/gerrit )
    Iaas概述
    题解西电OJ (Problem 1007 -做一名正气的西电人 )--长整型计算
    题解西电OJ (Problem 1005 -跳舞毯)--动态规划
    题解西电OJ (Problem 1004 -亚特兰提斯)--最小生成树
    题解西电OJ (Problem 1003 -最喜欢的数字)--动态规划
    题解西电OJ (Problem 1008
    题解西电OJ (Problem 1006
    HTML-css selector
    Android--应用开发3(Android layout XML属性)
  • 原文地址:https://www.cnblogs.com/yilinyangyu/p/8073423.html
Copyright © 2011-2022 走看看