wpf treeview 数据绑定 递归绑定节点

摘要:
1.先上效果将所有节点加入ComboBox数据源,在ComboBox中选择时下方Treeview显示该节点下的子节点。

1.先上效果

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

wpf treeview 数据绑定 递归绑定节点第1张

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 classGroup
2 {
3         publicGroup()
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 classMainWindow : Window
    {
        publicMainWindow()
        {
            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 inmainNodes)
            {
                grp.Nodes =getTreeData(grp.ID, otherNodes);
            }
            returnmainNodes;
        }

        /// <summary>
        ///下拉框关闭事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void cmbGoup_DropDownClosed(objectsender, 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;
        }
    }

免责声明:文章转载自《wpf treeview 数据绑定 递归绑定节点》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇Jenkins学习之——(2)插件的安装彻底搞定Android开发中软键盘的常见问题下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

WPF开发进阶

前一篇 简单的介绍了Fody/PropertyChanged的使用方法, 这一篇,我们详细介绍它的一些比较重要的特性和规则 1. Attributes 通过在类或属性上标记这些特性,可以在编译代码时,注入特定的功能 ImplementPropertyChangedAttribute 为类标记此特性,可以实现INotifyPropertyChanged接口...

WPF 任务栏图标闪烁提醒

1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Runtime.InteropServices; 5 using System.Text; 6 using System.Windows; 7 usin...

oracle start with connect by prior 递归查询用法

start with 子句:遍历起始条件,有个小技巧,如果要查父结点,这里可以用子结点的列,反之亦然。 connect by 子句:连接条件。关键词prior,prior跟父节点列parentid放在一起,就是往父结点方向遍历;prior跟子结点列subid放在一起,则往叶子结点方向遍历,                          parentid...

WPF RadioButton 绑定枚举

定义枚举类型 public enum CoordinateEnum { X=0,Y,Z,RX,RY,RZ } 定义枚举转换Convert public class EnumConvert : IValueConverter { public object Convert(object value,...

根据ID和parentID利用Java递归获取全路径名称

如下图所示,本文参考资源:https://jie-bosshr.iteye.com/blog/1996607 感谢大佬的无私奉献。 思路:定义一个方法getParentName参数为int类型的configId,返回类型为String类型.在方法getParentName内部进行如下操作:1 根据当前节点configId查询数据库,得到一条记录,存入实体...

JAVA递归生成树形菜单

  递归生成一个如图的菜单,编写两个类数据模型Menu、和创建树形的MenuTree。通过以下过程实现:     1.首先从菜单数据中获取所有根节点。     2.为根节点建立次级子树并拼接上。     3.递归为子节点建立次级子树并接上,直至为末端节点拼接上空的“树”。   首先,编写数据模型Menu。每条菜单有自己的id、父节点parentId、菜单...