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. PAT Basic 1032

    1032 挖掘机技术哪家强 (20 分) 为了用事实说明挖掘机技术到底哪家强,PAT 组织了一场挖掘机技能大赛.现请你根据比赛结果统计出技术最强的那个学校. 输入格式: 输入在第 1 行给出不超过 1 ...

  2. selenium3+java+POM 跨浏览器测试之------读取配置文件

    我们知道,web 测试的时候是需要切换不同的浏览器以查看每个功能在不同浏览器上的运行情况,使得开发的程序更具有健壮性.本文先总结一下如何通过读取配置文件来切换浏览器. 具体步骤如下: 一.编写配置文件 ...

  3. C_输入一个整数N,输出从0~N(算法思考)

    1.for循环实现 #include <stdio.h> #include <time.h> clock_t start, stop; double duration; voi ...

  4. 关于DTO的理解

    转自大神loveis715博文:http://www.cnblogs.com/loveis715/p/4379656.html 在一个web服务的实现中,我们常常需要访问数据库,并将从数据库中取得的数 ...

  5. KMP Algorithm 字符串匹配算法KMP小结

    这篇小结主要是参考这篇帖子从头到尾彻底理解KMP,不得不佩服原作者,写的真是太详尽了,让博主产生了一种读学术论文的错觉.后来发现原作者是写书的,不由得更加敬佩了.博主不才,尝试着简化一些原帖子的内容, ...

  6. duilib 新增数据迁移界面

    xml界面配置: <?xml version="1.0" encoding="utf-8"?> <Window caption="0 ...

  7. CSS---光标cursor设置、浮动布局与clear的关系

    光标设置 {cursor:auto;}--光标根据需要自动变化. {cursor:crosshair;}--光标变成“+”. {cursor:pointer;}--光标变成手指模式. {cursor: ...

  8. Operating Systems (COMP2006)

    Operating Systems (COMP2006) 1st Semester 2019Page 1, CRICOS Number: 00301JOperating Systems (COMP20 ...

  9. JavaScript中的this所引用的对象和如何改变这个引用

    this是函数内部的一个特殊对象,它引用的是函数执行环境对象.也就是运行是基于函数的执行环境绑定. 1.在网页全局作用域中调用函数时,this引用window var color='black'; f ...

  10. 汇编-13.0-int指令

    1.int指令 int指令的格式为:int n,n为中断类型码,它的功能是引发中断过程. 执行int n指令,相当于引发一个中断号为n的中断过程. (1).取中断类型码n: (2).标志寄存器入栈,I ...