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="300" Width="300" 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="16" Height="16" />-->
<!--<Image Source="Resources/KnowDot.png" Width="16" Height="16" />-->
<Image Source="/WpfTest;Component/Resources/KnowDot.png" Width="16" Height="16" />
<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; }
}
}

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

  1. 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. CAS 单点登录【2】自定义用户验证

       基础不太熟的同学可以先去看:CAS 单点登录[1]入门 方案1:CAS默认的JDBC扩展方案: CAS自带了两种简单的通过JDBC方式验证用户的处理器. 1.QueryDatabaseAuthe ...

  2. SpringBoot乱码

    第一步: 第一步,约定好传参编码格式 不管是运用httpclient,还是原生http,都要设置传参的编码,为了统一,这儿全部设置为utf-8 第二步,修正application.properties ...

  3. 关于web项目创建后WEB-INF下面没有出现web.xml的解决方法

    提供两种解决方案: 第一种:创建完项目后,需要手动创建出web.xml 第一步:选取创建的项目名称右击 第二步:eclipse的同学找到 java EE Tools 中的 下图画圈部分.  MyEcl ...

  4. ETF计划Q&A

    ETF计划Q&A 2018-07-16 参考:详解ETF计划.ETF计划Q&A(2017版) 目录 问1:ETF计划是什么?问2:ETF计划适合什么人参加?问3:我想参考你的计划,但告 ...

  5. golang:常量

    今天写代码的时候才发现,go语言里面的常量不能是数组(例如:[2]byte) 于是想查一下资料搞清楚到底是什么原因导致的,从effective go查到如下介绍: 但是这里也仅仅就是介绍了一下常量类型 ...

  6. 【OCR技术系列之七】端到端不定长文字识别CRNN算法详解

    在以前的OCR任务中,识别过程分为两步:单字切割和分类任务.我们一般都会讲一连串文字的文本文件先利用投影法切割出单个字体,在送入CNN里进行文字分类.但是此法已经有点过时了,现在更流行的是基于深度学习 ...

  7. C#学习笔记(35)——事件做的登录案例

    说明(2018-4-9 20:11:42): 1. 先自定义了一个登录控件,可以输入账号.密码,点击登录.然后在Form1里面拖入这个控件,要求输入账号密码正确时,点击登录,控件显示绿色,否则显示红色 ...

  8. 六、编写第一个应用【外部nodejs调用】

    一. 参考地址:https://hyperledger-fabric.readthedocs.io/en/latest/write_first_app.html 根据前几节的配置 1.下载代码 git ...

  9. Elasticsearch学习笔记——分词

    1.测试Elasticsearch的分词 Elasticsearch有多种分词器(参考:https://www.jianshu.com/p/d57935ba514b) Set the shape to ...

  10. 【GIS】无人机相关技术(转)

    ---------------------------------------------------------------------------------------------------G ...