<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. Selenium 自动化测试中对页面元素的value比较验证 java语言

    源代码: public boolean verifyText(String elementName, String expectedText) {String actualText = getValu ...

  2. C语言中函数的返回值

    规则 除局部变量的内存地址不能作为函数的返回值外,其他类型的局部变量都能作为函数的返回值. 我总结出下面这些规则: int.char等数据类型的局部变量可以作为函数返回值. 在函数中声明的指针可以作为 ...

  3. 开源框架是如何使用设计模式的-MyBatis缓存机制之装饰者模式

    写在前面 聊一聊MyBatis是如何使用装饰者模式的,顺便回顾下缓存的相关知识,可以看看右侧目录一览内容概述. 装饰者模式 这里就不了它的概念了,总结下就是套娃.利用组合的方式将装饰器组合进来,增强共 ...

  4. PHP的图片转base64,base64图片转换为图片并保存代码

    打卡记录 1. 图片转base64代码 /*图片转换为 base64格式编码*/ $img = 'images/avatar.jpg'; $base64_img = base64EncodeImage ...

  5. 【阅读笔记】Java核心技术卷一 #3.Chapter5

    5 继承 5.1 类.超类和子类 5.1.1 定义子类 超类(superclass)和子类(subclass), 基类(base class)和派生类(derived class), 父类(paren ...

  6. react踩坑笔记

    1.create-react-app中配置webpack // 方法一:将项目的配置文件抽取到项目中,即运行: npm run eject // 方法二:使用react-app-rewired 2.c ...

  7. ThinkPHP 5

    use think\Controller 1.$this->request->param();      内置request 安全对象, 不再使用 $_GET ,$_POST 2.path ...

  8. 关于win7+cenos 7双系统安装

    ---恢复内容开始--- 1,cenos 0 7制作U盘启动 制作工具 http://pan.baidu.com/s/1nv9lpmp 镜像自备 2,安装centos 7 释放磁盘空间,如:20G.用 ...

  9. Java8 Lambda表达式(一)

    目录 一.应用场景引入 优化一:使用策略模式 优化二:使用匿名内部类 优化三:使用Lambda表达式 优化四:使用Stream API 二.Lambda运算符和对应语法 语法格式 Lambda表达式需 ...

  10. scrapy 使用crawlspider rule不起作用的解决方案

    一直用的是通用spider,今天刚好想用下CrawlSpider来抓下数据.结果Debug了半天,一直没法进入详情页的解析逻辑.. 爬虫代码是这样的 # -*- coding: utf-8 -*- i ...