两个实体,其实一个实体也能构造出来,我这里是为了增加一个 checkbox

//第一个实体

public class person
{
public int no { get; set; }
public string name { get; set; }
public ObservableCollection<person> child { get; set; }
}

//第二个实体

public class inst                                       ****
{
public int instNo { get; set; }       ****
public string instName { get; set; }    ****
public int personID { get; set; }      ***
}

//xaml界面就放了一个  treeview

<Grid x:Name="LayoutRoot" Background="White">
<sdk:TreeView x:Name="tvshow" HorizontalAlignment="Left" Height="280" Margin="10,10,0,0" VerticalAlignment="Top" Width="367"/>

</Grid>

//cs页面

ObservableCollection<person> List = null;//第一个实体的集合
ObservableCollection<inst> instList = null;//第二个实体的集合      ****
public MainPage()
{
InitializeComponent();
List = new ObservableCollection<person>();
instList = new ObservableCollection<inst>();        ****
person p1 = new person() { no=1,name="1",child=new ObservableCollection<person>()};
person p2 = new person() { no = 2, name = "2", child = new ObservableCollection<person>() };
person p3 = new person() { no = 3, name = "3", child = new ObservableCollection<person>() };
person p4 = new person() { no = 4, name = "4", child = new ObservableCollection<person>() };
person p5 = new person() { no = 5, name = "5", child = new ObservableCollection<person>() };

inst inst1 = new inst() { instNo = 1,personID=1 ,instName = "jigou1" };     ***
inst inst2 = new inst() { instNo = 2, personID = 1, instName = "jigou2" };  ***
inst inst3 = new inst() { instNo = 3, personID = 1, instName = "jigou3" };  ***
inst inst4 = new inst() { instNo = 4, personID = 1, instName = "jigou4" };  ***
inst inst5 = new inst() { instNo = 5, personID = 2, instName = "jigou5" };  ***
inst inst6 = new inst() { instNo = 6, personID = 3, instName = "jigou6" };  ***
inst inst7 = new inst() { instNo = 7, personID = 4, instName = "jigou7" };  ***
p3.child.Add(p4);
p2.child.Add(p3);
List.Add(p1);
List.Add(p2);
List.Add(p5);

instList.Add(inst1); instList.Add(inst2); instList.Add(inst3); instList.Add(inst4); instList.Add(inst5); instList.Add(inst6); instList.Add(inst7);   ***

//生成树
foreach (var item in List)
{
tvshow.Items.Add(initView(item));
}
}

//递归函数
TreeViewItem initView(person p)
{
TreeViewItem it = new TreeViewItem();
it.Header = p.name;
foreach (var item in instList)
{
if (p.no == item.personID)                       ***
{
TreeViewItem cbt = new TreeViewItem();   ***
CheckBox cb = new CheckBox();              ***
cb.Content = item.instName;                    ***
cbt.Header = cb;                                     ***
it.Items.Add(cbt);                                   ***
}
}
if (p.child.Count>0)
{
foreach (var item in p.child)
{
it.Items.Add(initView(item));
}
}
return it;
}

运行效果:

如果不想 进行 复杂的 构造, 只生成第一的  无限级树, 可以把我 后面加*号的 部分  注释掉,就可以了。

silverlight中递归构造无限级树treeview+checkbox的更多相关文章

  1. 构造无限级树的框架套路,附上python/golang/php/js实现

    目录 前言 需求 数据 结果 框架 递归框架 迭代框架 递归框架实现 python golang php js 迭代框架实现 python golang php js 前言 框架思维非常重要,和语言无 ...

  2. php递归实现无限级分类树

      作者: PHP中文网|标签:PHP 递归 无限级树|2017-5-18 18:09   无限级树状图可以说是无限级栏目的一个显著特征,我们接下来就来看看两种不同的写法. 一.数据库设计 1 2 3 ...

  3. C# 递归构造树状数据结构(泛型),如何构造?如何查询?

    十年河东,十年河西,莫欺少年穷. 学无止境,精益求精 难得有清闲的一上午,索性写篇博客. 首先,我们需要准备一张表,如下范例: create table TreeTable ( TreeId ) no ...

  4. Silverlight 中 TreeView 的数据绑定

    方法一:Silverlight使用XAML标记语言来编写,如果不使用XAML强大的绑定功能,实在是罪过.通过使用绑定,可以将UI与视图模型层分离,有利于系统的维护.作为Silverlight中比较有代 ...

  5. 将Xml文件递归加载到TreeView中

    #region [通过XDocument的方式将Xml文件递归到TreeView控件中] //读取Xml文件(XDocument) //1.加载Xml文件 XDocument  document=XD ...

  6. django中的构造字典(二级菜单,评论树,购物车)

    1.构造父子结构: 1.1需求样式 客户列表 customer_list /customer/list/ -----> 添加客户 customer_add /customer/add/ ---- ...

  7. thinkphp框架中使用递归实现无限级分类

    无限级分类在我们开发中显得举足轻重,会经常被人问到,而一般会用递归的方法来实现,但是递归又会难倒一批人.今天博主分享的这个稍微有点基础的phper都能学会,希望大家能喜欢. 一.先建立对应的数据库和表 ...

  8. 堆应用---构造Huffman树(C++实现)

    堆: 堆是STL中priority_queue的最高效的实现方式(关于priority_queue的用法:http://www.cnblogs.com/flyoung2008/articles/213 ...

  9. thinkphp中如何实现无限级分类?

    thinkphp中如何实现无限级分类? 一.总结 1.数据表设计+递归算法 二.php实现无限级分类实例总结 1.数据库数据如下: 2.任务需求:给一个id,求自己和所有父亲. 3.实现代码如下:th ...

随机推荐

  1. linux作业控制和文件系统

    一.作业控制 [root@tianyun ~]# sleep 2000运行一个程序,当前终端无法输入. 1  直接运行后台程序.暂停一个前台程序.[root@tianyun ~]# sleep 300 ...

  2. arm汇编指令--str ldr

    STR :把寄存器中的字保存到存储器(寄存器到存储器) 示例: STR R0,[R1],#8             :将R0中的字数据写入以R1为地址的存储器中,并将新地址R1+8写入R1.STR ...

  3. Linux基础之bash shell介绍及基本特性

    今天继续讲Linux基础知识,内容是关于bash shell的.分享以下bash shell的相关知识,例如基本特性等.  1.8)bash shell的介绍 1.8.1)什么是bash shell ...

  4. C#async/await心得

    结论: 异步方法的方法签名要加 async,否则就算返回 Task 也是普通方法. 调用异步方法,可以加 await 或不加 await,两者方式都是马上返回,不加 await 得到的是 Task 对 ...

  5. JDK的命令行工具系列 (二) javap、jinfo、jmap

    javap: 反编译工具, 可用来查看java编译器生成的字节码 参数摘要: -help 帮助 -l 输出行和变量的表 -public 只输出public方法和域 -protected 只输出publ ...

  6. 自定义 EditText 样式

    极力推荐文章:欢迎收藏 Android 干货分享 阅读五分钟,每日十点,和您一起终身学习,这里是程序员Android 本篇文章主要介绍 Android 开发中的部分知识点,通过阅读本篇文章,您将收获以 ...

  7. ASP.NET Core on K8S深入学习(3)Deployment

    上一篇<部署过程解析与安装Dashboard>中我们了解K8S的部署过程,这一篇我们来了解一下K8S为我们提供的几种应用运行方式:Deployment.DaemonSet与Job,它们是K ...

  8. CSS3: @font-face 介绍与使用

    @font-face 是CSS3中的一个模块,他主要是把自己定义的Web字体嵌入到你的网页中,随着@font-face模块的出现,我们在Web的开发中使用字体不怕只能使用Web安全字体,你们当中或许有 ...

  9. 解决oh-my-zsh中git分支显示乱码问题

    oh-my-zsh显示github分支时,如果当前文件夹不是git仓库,它就会显示乱码.倒腾了好几个小时终于弄清楚是oh-my-zsh中函数”git_prompt_info“的锅,然后又花了半个多小时 ...

  10. 集成方法 Ensemble

    一.bagging 用于基础模型复杂.容易过拟合的情况,用来减小 variance(比如决策树).基础模型之间没有太多联系(相对于boosting),训练可以并行.但用 bagging 并不能有助于把 ...