<Window x:Class="TreeTest.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:TreeTest"
mc:Ignorable="d"
Title="MainWindow" Height="650" Width="400">
<Grid>
<Grid.Resources>
<HierarchicalDataTemplate DataType="{x:Type local: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="/TreeTest;Component/Resources/KnowDot.png" Width="16" Height="16" />
<TextBlock Text="{Binding Name}" ToolTip="{Binding Name}" Tag="{Binding}"/>
</StackPanel>
</HierarchicalDataTemplate>
</Grid.Resources>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition Height="150"/>
</Grid.RowDefinitions>
<TreeView Name="TreeView"/>
<TextBox Grid.Row="1" DataContext="{Binding ElementName=TreeView, Path=SelectedItem}" Text="{Binding Desp, Mode=TwoWay}"> </TextBox>
<Button Grid.Row="1" Width="100" Margin="282,10,10,-10" Click="Button_Click"></Button>
</Grid>
</Window>
using System.Collections.Generic;
using System.Windows; namespace TreeTest
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
List<Node> outputList = new List<Node>();
public MainWindow()
{
InitializeComponent();
List<Node> nodes = new List<Node>()
{
new Node { ID = 1, Name = "中国" },
new Node { ID = 2, Name = "北京市", ParentID = 1, Desp = "北京描述" },
new Node { ID = 3, Name = "吉林省", ParentID = 1, Desp = "吉林描述" },
new Node { ID = 4, Name = "上海市", ParentID = 1 , Desp = "上海描述" },
new Node { ID = 5, Name = "海淀区", ParentID = 2 },
new Node { ID = 6, Name = "朝阳区", ParentID = 2 },
new Node { ID = 7, Name = "大兴区", ParentID = 2 },
new Node { ID = 8, Name = "白山市", ParentID = 3 },
new Node { ID = 9, Name = "长春市", ParentID = 3 },
new Node { ID = 10, Name = "抚松县", ParentID = 8 },
new Node { ID = 11, Name = "靖宇县", ParentID = 8 },
new Node { ID = 12, Name = "美国" },
new Node { ID = 13, Name = "南美洲", ParentID = 12},
new Node { ID = 14, Name = "纽约", ParentID = 13 },
};
// 绑定树
outputList = Bind(nodes);
//(TreeView.SelectedItem as Node).ID
this.TreeView.ItemsSource = outputList;
}
/// <summary>
/// 绑定树
/// </summary>
List<Node> Bind(List<Node> nodes)
{
List<Node> outputList = new List<Node>();
for (int i = 0; i < nodes.Count; i++)
{
if (nodes[i].ParentID == -1)
{
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 = 0; 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;
} private void Button_Click(object sender, RoutedEventArgs e)
{
int a = 3;
}
} public class Node
{
public Node()
{
this.Nodes = new List<Node>();
this.ParentID = -1;
Desp = "描述";
}
public int ID { get; set; }
public string Name { get; set; }
public int ParentID { get; set; }
public string Desp { get; set; }
public List<Node> Nodes { get; set; }
}
}

WPF Tree多级绑定的更多相关文章

  1. WPF数据双向绑定

    设置双向绑定,首先控件要绑定的对象要先继承一个接口: INotifyPropertyChanged 然后对应被绑定的属性增加代码如下: 意思就是当Age这个属性变化时,要通知监听它变化的人. 即:Pr ...

  2. WPF的DataTrigger绑定自身属性

    原文:WPF的DataTrigger绑定自身属性 <DataTrigger Binding="{Binding RelativeSource={RelativeSource self} ...

  3. WPF 支持集合绑定的控件

    WPF 支持集合绑定的控件 ListBox ComboBox ListView DataGrid

  4. WPF UserControl 的绑定事件、属性、附加属性

    原文:WPF UserControl 的绑定事件.属性.附加属性 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/Vblegend_2013/arti ...

  5. 封装:WPF中可以绑定的BindPassWord控件

    原文:封装:WPF中可以绑定的BindPassWord控件 一.目的:本身自带的PassWord不支持绑定 二.Xaml部分 <UserControl x:Class="HeBianG ...

  6. WPF的DataGrid绑定ItemsSource后第一次加载数据有个别列移位的解决办法

    最近用WPF的DataGrid的时候,发现一个很弱智的问题,DataGrid的ItemsSource是绑定了一个属性: 然后取数给这个集合赋值的时候,第一次赋值,就会出现列移位 起初还以为是显卡的问题 ...

  7. 七,WPF的元素绑定

    数据绑定是一种关系,该关系告诉WPF从一个源对象提取一些信息,并使用这些信息设置目标对象的属性,目标属性总是依赖项属性,然而,源对象可以是任何内容. 源对象是WPF元素并且源属性是依赖项属性的数据绑定 ...

  8. C# Wpf集合双向绑定

    说明: msdn中   ObservableCollection<T> 类    表示一个动态数据集合,在添加项.移除项或刷新整个列表时,此集合将提供通知. 在许多情况下,所使用的数据是对 ...

  9. WPF学习:绑定

    原文 http://www.cnblogs.com/SouthAurora/archive/2010/06/30/1768464.html 一.绑定到元素对象 1.元素和元素(XAML.代码) 1.1 ...

随机推荐

  1. C语言:fopen函数

    在C语言中,操作文件之前必须先打开文件:所谓"打开文件",就是让程序和文件建立连接的过程.打开文件之后,程序可以得到文件的相关信息,例如大小.类型.权限.创建者.更新时间等.在后续 ...

  2. Java学习之注解篇

    Java学习之注解篇 0x00 前言 续上篇文章,这篇文章就来写一下注解的相关内容. 0x01 注解概述 Java注解(Annotation)又称Java标注,是JDK5.0约会的一种注释机制. 和J ...

  3. 2018年成为Web开发者的路线图

    本文通过一组大图展示了Web开发技能图谱,给出了作为Web 开发者可以采取的路径,以及总结了想要成为Web工程师的朋友们.希望和大家一起交流分享 介绍 Web 开发的角色一般说来,包括前端.后端和de ...

  4. [刘阳Java]_EasyUI环境搭建_第2讲

    在EasyUI的第1讲中我们介绍了学习EasyUI能够做什么,这次我们得快速搭建一个EasyUI环境,来测试一下它的运行效果 1.jQuery EasyUI环境搭建 <script type=& ...

  5. 用Nextcloud在树莓派上布置你的个人网盘“NAS”

    用Nextcloud在树莓派上布置你的个人网盘"NAS" 这次用的是目前最新的 Raspbian Stretch 系统,基于 Debian 9. 软件程序是 Nextcloud 1 ...

  6. 【洛谷P1318积水面积】最小生成树

    我写一篇绝对原创的题解,算法原创,求洛谷通过!!!(让更多人看到这篇题解) 绝大多数人肯定认为这道题是一道模拟题 以下为正解 我们来看一下这一道题,其实就是找到左右高点,在模拟. 但是这个是正常人的想 ...

  7. TCP协议与HTTP协议区别

    一.TCP协议与HTTP协议区别 1.直观认识 TCP协议对应于传输层,而HTTP协议对应于应用层,从本质上来说,二者没有可比性.Http协议是建立在TCP协议基础之上的,当浏览器需要从服务器获取网页 ...

  8. Spring 框架中都用到了哪些设计模式

    Spring 框架中都用到了哪些设计模式? Spring 框架中使用到了大量的设计模式,下面列举了比较有代表性的: 1.代理模式-在 AOP 和 remoting 中被用的比较多. 2.单例模式:在 ...

  9. phpmyadmin error:#2002 - 服务器没有响应 (或者本地 MySQL 服务器的套接字没有正确配置)

    1. 将 "phpMyAdmin/libraries"文件夹下的config.default.php文件中的$cfg['Servers'][$i]['host'] = 'local ...

  10. Flutter学习(7)——网络请求框架Dio简单使用

    原文地址: Flutter学习(7)--网络请求框架Dio简单使用 | Stars-One的杂货小窝 Flutter系列学习之前都是在个人博客发布,感兴趣可以过去看看 网络请求一般APP都是需要的,在 ...