1.先上效果

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

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

     <StackPanel>
<StackPanel Margin="10">
<Label Content="选择组节点:"></Label>
<ComboBox MaxDropDownHeight="100" Name="cmbGoup" DropDownClosed="cmbGoup_DropDownClosed"></ComboBox>
</StackPanel>
<StackPanel Margin ="10">
<TreeView x:Name="tvGroup">
<TreeView.ItemTemplate>
<HierarchicalDataTemplate ItemsSource="{Binding Nodes}">
<StackPanel>
<TextBlock VerticalAlignment="Center" FontSize="14" Text="{Binding GroupName}" Margin="2,0,0,0"></TextBlock>
</StackPanel>
</HierarchicalDataTemplate>
</TreeView.ItemTemplate>
</TreeView>
</StackPanel>
</StackPanel>

2.后台代码

a.用于绑定的节点类

     public class Group
{
public Group()
{
this.Nodes = new List<Group>();
this.ParentId = ;//主节点的父id默认为0
} public List<Group> Nodes { get; set; }
public int ID { get; set; }//id
public int ParentId { get; set; }//parentID
public string GroupName { get; set; }
}

b.主界面类代码

public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent(); #region 用于绑定的数据
List<Group> grpLst = new List<Group>(){
new Group(){ID=,GroupName="Group", ParentId = -},
new Group(){ID=,GroupName="Group1",ParentId=},
new Group(){ID=,GroupName="Group2",ParentId=},
new Group(){ID=,GroupName="Group1_1",ParentId=},
new Group(){ID=,GroupName="Group1_2",ParentId=},
new Group(){ID=,GroupName="Group1_3",ParentId=},
new Group(){ID=,GroupName="Group1_4",ParentId=},
new Group(){ID=,GroupName="Group1_5",ParentId=},
new Group(){ID=,GroupName="Group2_1",ParentId=},
new Group(){ID=,GroupName="Group2_2",ParentId=},
new Group(){ID=,GroupName="Group2_3",ParentId=},
new Group(){ID=,GroupName="Group2_4",ParentId=},
new Group(){ID=,GroupName="Group1_1_1",ParentId=},
new Group(){ID=,GroupName="Group1_1_2",ParentId=},
new Group(){ID=,GroupName="Group1_2_1",ParentId=},
new Group(){ID=,GroupName="Group1_1_1_1",ParentId=}
};
#endregion this.cmbGoup.ItemsSource = grpLst;//comboBox数据源
this.cmbGoup.SelectedValuePath = "ID";
this.cmbGoup.DisplayMemberPath = "GroupName"; List<Group> lstGroup = getTreeData(-, 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;
}
}

wpf treeview 数据绑定 递归绑定节点的更多相关文章

  1. WPF—TreeView无限极绑定集合形成树结构

    1.如图所示:绑定树效果图 2.前台Xaml代码: <Window x:Class="WpfTest.MainWindow" xmlns="http://schem ...

  2. [No0000D1]WPF—TreeView无限极绑定集合形成树结构

    1.如图所示:绑定树效果图 2.前台Xaml代码: <Window x:Class="WpfTest.MainWindow" xmlns="http://schem ...

  3. WPF TreeView 展开到指定节点

    最近在做一个交换机管理的项目,有一个交换机的树,做树的搜索的时候 展开节点居然有点难,自己记录下来 ,以后用的到的时候可以看一下. 展开代码如下,其中 SwitchTree是treeview空间的名称 ...

  4. ASP.NET树形控件TreeView的递归绑定

    来自:http://blog.csdn.net/xqf003/article/details/4958727

  5. 潜移默化学会WPF(难点控件treeview)--改造TreeView(CheckBox多选择版本),递归绑定数据

    原文:潜移默化学会WPF(难点控件treeview)--改造TreeView(CheckBox多选择版本),递归绑定数据 目前自己对treeview的感慨很多 今天先讲 面对这种 表结构的数据 的其中 ...

  6. TreeView树形控件递归绑定数据库里的数据

    TreeView树形控件递归绑定数据库里的数据. 第一种:性能不好 第一步:数据库中查出来的表,字段名分别为UNAME(显示名称),DID(关联数据),UTYPE(类型) 第二步:前台代码 <% ...

  7. TreeView递归绑定无限分类数据

    TreeView递归绑定无限分类数据 实现一个动态绑定,无限级分类数据时,需要将数据绑定到TreeView控件,分类表的结构是这样的: 字段 类型 Id int ParentId int Name N ...

  8. WPF 组织机构下拉树多选,递归绑定方式现实

    使用HierarchicalDataTemplate递归绑定现实 XAML代码: <UserControl x:Class="SunCreate.CombatPlatform.Clie ...

  9. WPF TreeView Indent 减少节点的缩进

    www.swack.cn - 原文链接:WPF TreeView Indent 减少节点的缩进 问题 最近一个需求,需要在界面中实现Windows资源管理器TreeView的界面.但是我发现,我做出的 ...

随机推荐

  1. Python3的桌面程序开发利器:Eric6的环境搭建、使用

    本文旨在通过一个简单的demo,介绍基于Python3.PyQT5的环境下开发桌面应用程序的一种方案,当然开发Python的桌面应用程序不止是PyQT 这一种方案,还可以使用Python自带的Tkin ...

  2. (77)Wangdao.com第十五天_JavaScript 用于数据交换的文本格式 JSON 对象

    JSON 对象 JSON (JavaScript Object Notation 的缩写) 也是一种数据,是 JavaScript 的原生对象,用来处理 JSON 格式数据.它有两个静态方法:JSON ...

  3. react_app 项目开发 (5)_前后端分离_后台管理系统_开始

    项目描述 技术选型 react API 接口 接口文档,url,请求方式,参数类型, 根据文档描述的方法,进行 postman 测试,看是否能够得到理想的结果 collections - 创建文件取项 ...

  4. [LeetCode] Number of Subarrays with Bounded Maximum 有界限最大值的子数组数量

    We are given an array A of positive integers, and two positive integers L and R (L <= R). Return ...

  5. javascript的数组之splice()

    splice()方法通过删除现有元素和/或添加新元素来更改一个数组的内容.修改数组自身 var months = ['Jan', 'March', 'April', 'June']; months.s ...

  6. SSH集成(Struts+Spring+Hibernate)

    环境:struts2.3.Xspring4.0.0hibernate4.2 思路:从下开始往上集成;层与层之间没有关系;在集成的时候,只关注当前集成的那个层的内容; 1,创建一个空的web项目;重新定 ...

  7. Jumpserver之快速入门

    一,系统设置 1.1基本设置 修改 url 的"localhost"为你的实际 url 地址, 否则邮件收到的地址将为"localhost" 也无法创建新用户 ...

  8. vue中路由懒加载实现amd加载文件

    一般我们配置路由的时候是import引入: import log from '@/components/login': { path: '/login', component: log , hidde ...

  9. JAVAjdk第一次作业

    第一次的作业由于对JAVA程序的不熟悉以及jdk环境变量的配置问题,第一次的作业写了很久

  10. [dev][socket] unix domain socket删除socket文件

    问题 在使用unix domain socket的时候,bind之后,会在本地路径里 产生一个与path对应的socket文件. 如何正确的在用完socket之后,对其销毁呢? 方案 使用 unlin ...