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——简介
1.django的介绍 Django是一个开放源代码的Web应用框架,由Python写成.采用了MVT的软件设计模式,即模型Model,视图View和模板Template.它最初是被开发来用于管理劳伦 ...
- Understanding HBase and BigTable
Hbase is a distributed data storage systems. A Bigtable is spare , distributed , persistent multidim ...
- Java 标记接口
没有声明或定义方法的接口称为标记接口(Mark Interface).某个类实现该接口时不需要重写方法,表明具有接口标记的功能.Java中常用的3个标记接口如下: 1 Serializable jav ...
- windows与linux之间文件的传输
这边记录一下如何在windows与linux之间进行文件的传输,下面是具体的网址. 原文地址::http://blog.csdn.net/shufac/article/details/51966276 ...
- __x__(3)0905第二天__W3C标准集合
World Wide Web Consortium 万维网联盟(外语缩写:W3C)标准不是某一个标准,而是一系列标准的集合. 创建于 1994 年,是 Web 技术领域最具权威和影响力的国际中立性技术 ...
- 09_ for 练习 _ FlowerNumber
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title&g ...
- mobile_视口
document.documentElement.clientWidth 不包含滚动条 window.innerWidth ...
- React_基本原理_ajax
React 基本原理 初始化显示界面 创建虚拟DOM树 渲染到 原生 DOM 树 绘制界面显示 更新界面 setState() 更新状态机 重新创建虚拟 DOM 树 新/旧树比较差异 (执行一次 DO ...
- Linux安装nginx详细步骤
安装依赖 yum -y install gcc zlib zlib-devel pcre-devel openssl openssl-devel 在/usr/local/下创建一个nginx的文件夹 ...
- Bear + Reminders 是完美的Thing 3 的替代品
如今同类功能的APP在AppStore上呈现泛滥之势,尤其是时间管理.任务管理之类的APP.其中比较出名的就有“Things 3”这款APP,这是一款多年不更新,一更新就获奖的APP.目前在AppSt ...