wpf treeview 数据绑定 递归绑定节点
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 数据绑定 递归绑定节点的更多相关文章
- WPF—TreeView无限极绑定集合形成树结构
1.如图所示:绑定树效果图 2.前台Xaml代码: <Window x:Class="WpfTest.MainWindow" xmlns="http://schem ...
- [No0000D1]WPF—TreeView无限极绑定集合形成树结构
1.如图所示:绑定树效果图 2.前台Xaml代码: <Window x:Class="WpfTest.MainWindow" xmlns="http://schem ...
- WPF TreeView 展开到指定节点
最近在做一个交换机管理的项目,有一个交换机的树,做树的搜索的时候 展开节点居然有点难,自己记录下来 ,以后用的到的时候可以看一下. 展开代码如下,其中 SwitchTree是treeview空间的名称 ...
- ASP.NET树形控件TreeView的递归绑定
来自:http://blog.csdn.net/xqf003/article/details/4958727
- 潜移默化学会WPF(难点控件treeview)--改造TreeView(CheckBox多选择版本),递归绑定数据
原文:潜移默化学会WPF(难点控件treeview)--改造TreeView(CheckBox多选择版本),递归绑定数据 目前自己对treeview的感慨很多 今天先讲 面对这种 表结构的数据 的其中 ...
- TreeView树形控件递归绑定数据库里的数据
TreeView树形控件递归绑定数据库里的数据. 第一种:性能不好 第一步:数据库中查出来的表,字段名分别为UNAME(显示名称),DID(关联数据),UTYPE(类型) 第二步:前台代码 <% ...
- TreeView递归绑定无限分类数据
TreeView递归绑定无限分类数据 实现一个动态绑定,无限级分类数据时,需要将数据绑定到TreeView控件,分类表的结构是这样的: 字段 类型 Id int ParentId int Name N ...
- WPF 组织机构下拉树多选,递归绑定方式现实
使用HierarchicalDataTemplate递归绑定现实 XAML代码: <UserControl x:Class="SunCreate.CombatPlatform.Clie ...
- WPF TreeView Indent 减少节点的缩进
www.swack.cn - 原文链接:WPF TreeView Indent 减少节点的缩进 问题 最近一个需求,需要在界面中实现Windows资源管理器TreeView的界面.但是我发现,我做出的 ...
随机推荐
- Django——图书管理系统
基于Django的图书管理系统 1.主体功能 1.列出图书列表.出版社列表.作者列表 2.点击作者,会列出其出版的图书列表 3.点击出版社,会列出旗下图书列表 4.可以创建.修改.删除 图书.作者.出 ...
- 转:python request属性及方法说明
转:http://blog.csdn.net/u013398398/article/details/52214582 if request.REQUEST.has_key('键值'): HttpRe ...
- jdk1.8.0_40 +maven+tomcat7.0+mysql8.0详细安装教程
(一) jdk的安装 1.下载jdk推荐下载地址:http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-213 ...
- 关于ajaxFileUpload图片上传,success和error都触发的情况
最近做到项目使用JQuery的插件ajaxFileUpload~~~ 遇到了非常领人匪夷所思的事情,当图片上传成功之后呢(success),它的error事件也被触发了,情况就是: 后端数据正确返回 ...
- React(四)组件生命周期
组件的生命周期可分成三个状态: Mounting:已插入真实 DOM Updating:正在被重新渲染 Unmounting:已移出真实 DOM 生命周期的方法有: componentWillMoun ...
- laravel之数据库
mysql数据库设置其实在.env中 数据库修改在
- .node 文件require时候显示Error: The specified module could not be found
参考文章:https://stackoverflow.com/questions/41253450/error-the-specified-module-could-not-be-found 第一:你 ...
- 配置ssh框架启动tomcat服务器报异常Unable to create requested service [org.hibernate.engine.jdbc.env.spi.JdbcEnvironment]
在Spring中配置jdbc时,引用的是dbcp.jar包,在db.properties配置文件中,使用了之前的properties配置文件的用户名username(MySql用户名) 然后在启动服务 ...
- vue发送请求----vue-resource
使用插件vue-resource 官方提供的接口,在vue官网找不到 但在github中可以找到 安装:cnpm install vue-resource --save 第一步:注意要加--save, ...
- centos开启防火墙
https://www.cnblogs.com/oskyhg/p/8011001.html