<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. python使用笔记24--面向对象编程2

    类方法 类里面自带的方法,不用实例化就可以调用,想象,模型上自带的功能 类方法是公共的,在实例方法里面可以随意调用 但是在类方法里不能调用实例方法,不能使用实例变量,但是他可以调用其他的类方法 1 @ ...

  2. C语言:2.1

    int main() { char zi='A'; short bla=10; int blb=20; long blc=30; float bld=340.56; double ble=34.324 ...

  3. C语言:c++ ++c

    #include <stdio.h> int main() {int a=3,x; x=(a++)+(++a)+(++a); printf("%d",x); getch ...

  4. Python3.7 lxml引入etree

    用xml代替lxml,Python3.7中已经没有etree这个模块了 import xml.etree.ElementTree as etree from lxml import etree 这种方 ...

  5. 结对开发_石家庄地铁查询web系统_psp表

    结对开发_石家庄地铁查询_博客地址:https://www.cnblogs.com/flw0322/p/10680172.html PSP0: PSP0 Personal Software Proce ...

  6. Python+js进行逆向编程加密MD5格式

    一.安装nodejs 二.安装:pip install PyExecJs 三.js源文件Md5格式存放本地,如下 var n = {}function l(t, e) {var n = (65535 ...

  7. python3实现名片管理系统(文件版)

    def menu(): #首先定义功能列表函数menu() print(" 名片管理系统 V1.0 ") print("1:增加新用户") print(&quo ...

  8. GoldenEye-v1靶机

    仅供个人娱乐 靶机信息 下载地址:https://pan.baidu.com/s/1dzs_qx-YwYHk-vanbUeIxQ 一.主机扫描 二.信息收集 三.漏洞的查找和利用 boris    I ...

  9. Redis 实战篇:巧用数据类型实现亿级数据统计

    在移动应用的业务场景中,我们需要保存这样的信息:一个 key 关联了一个数据集合,同时还要对集合中的数据进行统计排序. 常见的场景如下: 给一个 userId ,判断用户登陆状态: 两亿用户最近 7 ...

  10. vivo 全球商城:优惠券系统架构设计与实践

    一.业务背景 优惠券是电商常见的营销手段,具有灵活的特点,既可以作为促销活动的载体,也是重要的引流入口.优惠券系统是vivo商城营销模块中一个重要组成部分,早在15年vivo商城还是单体应用时,优惠券 ...