1.如图所示:绑定树效果图

2.前台Xaml代码:

<Window x:Class="WpfTest.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mode="clr-namespace:WpfTest"
Title="TreeView无限级树绑定事例" Height="" Width="" WindowStartupLocation="CenterScreen" ResizeMode="CanMinimize">
<Grid>
<Grid.Resources>
<HierarchicalDataTemplate DataType="{x:Type mode:Node}" ItemsSource="{Binding Nodes}">
<StackPanel Orientation="Horizontal" Margin="0,2,0,2">
<!--<Image Source="pack://application:,,,/WpfTest;Component/Resources/KnowDot.png" Width="" Height="" />-->
<!--<Image Source="Resources/KnowDot.png" Width="" Height="" />-->
<Image Source="/WpfTest;Component/Resources/KnowDot.png" Width="" Height="" />
<TextBlock Text="{Binding Name}" ToolTip="{Binding Name}" Tag="{Binding}"/>
</StackPanel>
</HierarchicalDataTemplate>
</Grid.Resources>
<TreeView Name="TreeView"/>
</Grid>
</Window>

2.后台cs代码:采用递归无限极向下查询

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Threading;
using System.Diagnostics;
using System.Windows.Markup; namespace WpfTest
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
List<Node> nodes = new List<Node>()
{
new Node { ID = , Name = "中国" },
new Node { ID = , Name = "北京市", ParentID = },
new Node { ID = , Name = "吉林省", ParentID = },
new Node { ID = , Name = "上海市", ParentID = },
new Node { ID = , Name = "海淀区", ParentID = },
new Node { ID = , Name = "朝阳区", ParentID = },
new Node { ID = , Name = "大兴区", ParentID = },
new Node { ID = , Name = "白山市", ParentID = },
new Node { ID = , Name = "长春市", ParentID = },
new Node { ID = , Name = "抚松县", ParentID = },
new Node { ID = , Name = "靖宇县", ParentID = }
};
// 绑定树
List<Node> outputList = Bind(nodes);
//(TreeView.SelectedItem as Node).ID
this.TreeView.ItemsSource = outputList;
//TreeViewItem item = new TreeViewItem();
//item.Header = "";
}
/// <summary>
/// 绑定树
/// </summary>
List<Node> Bind(List<Node> nodes)
{
List<Node> outputList = new List<Node>();
for (int i = ; i < nodes.Count; i++)
{
if (nodes[i].ParentID == -)
{
outputList.Add(nodes[i]);
}
else
{
FindDownward(nodes, nodes[i].ParentID).Nodes.Add(nodes[i]);
}
}
return outputList;
}
/// <summary>
/// 递归向下查找
/// </summary>
Node FindDownward(List<Node> nodes, int id)
{
if (nodes == null) return null;
for (int i = ; i < nodes.Count; i++)
{
if (nodes[i].ID == id)
{
return nodes[i];
}
Node node = FindDownward(nodes[i].Nodes, id);
if (node != null)
{
return node;
}
}
return null;
}
} public class Node
{
public Node()
{
this.Nodes = new List<Node>();
this.ParentID = -;
}
public int ID { get; set; }
public string Name { get; set; }
public int ParentID { get; set; }
public List<Node> Nodes { get; set; }
}
}

WPF—TreeView无限极绑定集合形成树结构的更多相关文章

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

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

  2. wpf treeview 数据绑定 递归绑定节点

    1.先上效果 将所有节点加入ComboBox数据源,在ComboBox中选择时下方Treeview显示该节点下的子节点. 1.xaml文件,将以下代码加入界面合适位置 <StackPanel&g ...

  3. 转:打造DropDownList,TreeView,ListBox无限极分类目录树

    [csharp] view plaincopyprint? #region DropDownList无限递归显示层次关系 /// <summary> /// 创建无限分级下拉列表框 /// ...

  4. WPF 绑定集合 根据集合个数改变样式 INotifyCollectionChanged

    问题:当前ListBox Items 绑定 集合数据源ListA时候:ListA集合数据源中存在另外一个集合ListB,当更改或往ListB集合中添加数据的时候,通知改变? 实体类继承 INotify ...

  5. WPF Binding学习(四) 绑定各种数据源

    转自:http://blog.csdn.net/lisenyang/article/details/18312199 1.集合作为数据源 首先我们先创建一个模型类 public class Stude ...

  6. WPF TreeView HierarchicalDataTemplate

    原文 WPF TreeView HierarchicalDataTemplate HierarchicalDataTemplate 的DataType是本层的绑定,而ItemsSource是绑定下层的 ...

  7. C#无限极分类树-创建-排序-读取 用Asp.Net Core+EF实现

    今天做一个管理后台菜单,想着要用无限极分类,记得园子里还是什么地方见过这种写法,可今天找了半天也没找到,没办法静下心来自己写了: 首先创建节点类(我给它取名:AdminUserTree): /// & ...

  8. [WPF 基础知识系列] —— 绑定中的数据校验Vaildation

    前言: 只要是有表单存在,那么就有可能有对数据的校验需求.如:判断是否为整数.判断电子邮件格式等等. WPF采用一种全新的方式 - Binding,来实现前台显示与后台数据进行交互,当然数据校验方式也 ...

  9. 【laravel5.4】Baum无限极分类和collect助手函数、transform()中间件(转换数据)方法使用

    1.目的,无限极分类 /* * getdepartment:获取[当前登录用户对应公司的所有有效部门] * DB::table ==>返回查询构造器结果,不会返回一个collect实例 * 而 ...

随机推荐

  1. 解决JSP 不解析EL表达式

    解决JSP 不解析EL表达式,jsp在使用EL表达式的时候发现它不被解析,而是直接以字符串的形式显示了出来,经过查阅资料和实践,终于得知了原因并找到了解决方案 原因是:在默认情况下,Servlet 2 ...

  2. 算法库:clapack安装配置

    类似于opencv.jpeglib和pnglib的安装配置. opencv安装配置见:http://www.cnblogs.com/dzyBK/p/4954945.html jpeglib和pngli ...

  3. UVA12653 Buses

    Problem HBusesFile: buses.[c|cpp|java]Programming competitions usually require infrastructure and or ...

  4. XML小总结

    XHTML 标签都有固定含义,不能去创造新的标签. 而 XML 支持自定义标签,具有扩 展性. 定义 XML 文档结构有两种方法:DTD 和 XSD. XSD 本身就是 XML 文档结构,是继 DTD ...

  5. 跨平台c++ Coroutine,仿unity3d实现

    不多说,贴代码: #include "stdafx.h" #include <list> #include <thread> #include <ch ...

  6. The BKS System for the Philco-2000 学习笔记

    最近因为学业需要阅读这一篇论文......一把鼻涕一把泪地翻了一边以求更好地理解,谁让我英语渣呢....有很多地方翻译得感觉还是有问题或者不流畅,还请大家多多指教. The BKS System fo ...

  7. nginx 配置文件参数说明

    #运行用户 user www-data;     #启动进程,通常设置成和cpu的数量相等 worker_processes  1; #全局错误日志及PID文件 error_log  /var/log ...

  8. 8051学习笔记——IIC与EEPROM实验

    main.c #include <reg51.h> #include "iic.h" #define AT24C02 0xa0 //AT24C02 地址 sbit LS ...

  9. socket学习笔记——select函数的使用(windows)

    #include <stdio.h> #include <stdlib.h> #include <string.h> #include <winsock2.h ...

  10. C# Exception 对象的属性

    关于 C# 中 Exception 对象的属性,在程序出现异常时,需要记录异常的信息,并把信息保存起来或输出,以方便准确定位异常的根源,以下是 Exception  对象的属性 名称          ...