<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使用笔记19--网络操作

    1.get请求 1 import requests 2 import datetime 3 #get请求 4 url = 'http://api.nnzhp.cn/api/user/stu_info' ...

  2. LeetCode 778. Swim in Rising Water

    题目链接:https://leetcode.com/problems/swim-in-rising-water/ 题意:已知一个n*n的网格,初始时的位置为(0,0),目标位置为(n-1,n-1),且 ...

  3. if函数+isna函数+vlookup函数实现不同列相同单元格内容排列在同一行

    1,首先学习的网址:https://jingyan.baidu.com/album/22a299b5dd0f959e19376a22.html?picindex=1 2,excel 这也许是史上最好最 ...

  4. pycharm基础使用入门

    pycharm基础使用入门 输出 print函数 print('hello world') 右键选择run或者右上角的三角形运行,可以运行出结果 "E:\all sorts of learn ...

  5. java常见的面试题(二)

    1.mybatis 中 #{}和 ${}的区别是什么? #{}是预编译处理,${}是字符串替换: Mybatis在处理#{}时,会将sql中的#{}替换为?号,调用PreparedStatement的 ...

  6. 极致简洁的微前端框架-京东MicroApp开源了

    前言 MicroApp是一款基于类WebComponent进行渲染的微前端框架,不同于目前流行的开源框架,它从组件化的思维实现微前端,旨在降低上手难度.提升工作效率.它是目前市面上接入微前端成本最低的 ...

  7. Linux下系统防火墙的发展历程和怎样学好防火墙(iptalbes和firewalld)

    有关firewalld和iptables详细使用的文章 iptables详解 firewalld详解 =====================================华丽的分割线====== ...

  8. Spring Cloud Alibaba基础教程:Nacos+Dubbo

    Spring Cloud Alibaba为分布式应用程序开发提供了一站式解决方案. 它包含开发分布式应用程序所需的所有组件,使您可以轻松地使用Spring Cloud开发应用程序.Dubbo是Alib ...

  9. jvm源码解读--01 jvm加载java/lang/object过程

    现在做一下记录,这个看了两天,看的过程发现了很多c++的高级特性,没接触过,还得慢慢撸,禁止很慢 那么现在开始 吧 先打两个断点 java.c:351 JavaMain(void * _args) { ...

  10. idea 提示不能打开cmd.exe,idea 编译项目 CreateProcess error=740, 请求的操作需要提升 --->如何设置cmd以管理员身份运行

    问题描述:idea 编译项目 idea 编译项目 CreateProcess error=740, 请求的操作需要提升 CreateProcess error=740, 请求的操作需要提升 全网搜索可 ...