WPF 目录树绑定 与 遍历
定义树节点,(编译环境VS2017)
public class GBTreeNode : INotifyPropertyChanged
{
private string _deviceId = string.Empty;
private string _name = string.Empty;
private string _parentId = string.Empty;
private ImageSource _imageSource = null;
private ObservableCollection<GBTreeNode> _Nodes = new ObservableCollection<GBTreeNode>();
public string DeviceId
{
set => UpdateProperty(ref _deviceId, value);
get => _deviceId;
}
public string Name
{
set => UpdateProperty(ref _name, value);
get => _name;
}
public string ParentId
{
set => UpdateProperty(ref _parentId, value);
get => _parentId;
}
public ImageSource ImageSource
{
set => UpdateProperty(ref _imageSource, value);
get => _imageSource;
}
public ObservableCollection<GBTreeNode> Nodes
{
set => UpdateProperty(ref _Nodes, value);
get => _Nodes;
}
public void UpdateProperty<T>(ref T properValue, T newValue, [CallerMemberName] string propertyName = "")
{
if (object.Equals(properValue, newValue))
{
return;
}
properValue = newValue; OnPropertyChanged(propertyName);
}
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged([CallerMemberName]string propertyName = "")
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
}
xmal 前端代码: localCatalog 绑定的树结构
<UserControl.Resources>
<HierarchicalDataTemplate x:Key="TreeViewTemplate" DataType="{x:Type mode:GBTreeNode}" ItemsSource="{Binding Nodes, Mode=TwoWay}">
<StackPanel Orientation="Horizontal" Margin="0,2,0,2">
<Image Source="{Binding ImageSource , Mode=TwoWay}" Width="16" Height="16" />
<Label Width="5" Opacity="0"></Label>
<TextBlock Text="{Binding Name, Mode=TwoWay}" ToolTip="{Binding Name, Mode=TwoWay}" FontSize="12"/>
</StackPanel>
</HierarchicalDataTemplate>
</UserControl.Resources>
<TreeView Background="#DBDDE1" ItemTemplate="{StaticResource ResourceKey=TreeViewTemplate}" BorderThickness="1"
BorderBrush="Red" ItemsSource="{Binding Path=localCatalog}" Margin="10,0,0,0" />
Tips: 编译的时候 DataType="{x:Type mode:GBTreeNode}" 此段代码会出错, 需要在xmal头添加GBTreeNode所在命名空间
xmlns:mode="clr-namespace:XXXXXXXXXXX"
CS中的后台代码: 生成目录树结构
public ObservableCollection<GBTreeNode> localCatalog = new ObservableCollection<GBTreeNode>();
localCatalog = Bind(所有的树节点);
//绑定树
private ObservableCollection<GBTreeNode> Bind(ObservableCollection<GBTreeNode> nodes)
{
ObservableCollection<GBTreeNode> outputList = new ObservableCollection<GBTreeNode>();
for (int i = 0; i < nodes.Count; i++)
{
if (FindDownward(nodes, nodes[i].ParentId) != null)
{
FindDownward(nodes, nodes[i].ParentId).Nodes.Add(nodes[i]);
}
else //找不到父节点,此节点就为根节点
{
outputList.Add(nodes[i]);
}
}
return outputList;
}
//向下查找
private GBTreeNode FindDownward(ObservableCollection<GBTreeNode> nodes, string ParentId)
{
if (nodes.Count == 0) return null;
for (int i = 0; i < nodes.Count; i++)
{
if (nodes[i].DeviceId.Equals(ParentId))
{
return nodes[i];
}
GBTreeNode node = FindDownward(nodes[i].Nodes, ParentId);
if (node != null)
{
return node;
}
}
return null;
}
CS后台代码: 遍历目录树结构
//nodes: 创建好的树结构 list: 保存树所有的树节点
private void RecurPackageTreeNodeXml(ObservableCollection<GBTreeNode> nodes, List<CatalogConfigItemType> list)
{
if (nodes.Count == 0) return;
foreach (GBTreeNode item in nodes)
{
/*
业务代码
*/
RecurPackageTreeNodeXml(item.Nodes, list);
list.Add(itemNode);
}
}
WPF 目录树绑定 与 遍历的更多相关文章
- WPF 4 目录树型显示
原文:WPF 4 目录树型显示 本篇将通过WPF4 制作简单的目录树型结构显示实例,完成本篇内容我们将作出下图所示的应用程序. 从图中我们可以看到程序主要分为两部分:左边显示本地驱 ...
- python GUI编程tkinter示例之目录树遍历工具
摘录 python核心编程 本节我们将展示一个中级的tkinter应用实例,这个应用是一个目录树遍历工具:它会从当前目录开始,提供一个文件列表,双击列表中任意的其他目录,就会使得工具切换到新目录中,用 ...
- 遍历目录树 - Unicode 模式
=info 遍历目录树 支持 Unicode Code by 523066680@163.com 2017-03 V0.5 使用Win32API判断目录硬链接 ...
- 利用树的先序和后序遍历打印 os 中的目录树
[0]README 0.1)本代码均为原创,旨在将树的遍历应用一下下以加深印象而已:(回答了学习树的遍历到底有什么用的问题?)你对比下linux 中的文件树 和我的打印结果就明理了: 0.2)我们采用 ...
- Android原生PDF功能实现:PDF阅读、PDF页面跳转、PDF手势伸缩、PDF目录树、PDF预览缩略图
1.背景 近期,公司希望实现安卓原生端的PDF功能,要求:高效.实用. 经过两天的调研.编码,实现了一个简单Demo,如上图所示. 关于安卓原生端的PDF功能实现,技术点还是很多的,为了咱们安卓开发的 ...
- 05.表达式目录树Expression
参考文章 https://www.cnblogs.com/xyh9039/p/12748983.html 1. 基本了解 1.1 Lambda表达式 演变过程 using System; namesp ...
- C#表达式目录树(Expression)
1.什么是表达式目录树 :简单的说是一种语法树,或者说是一种数据结构(Expression) 2.用Lambda声明表达式目录树: Expression<Func<; //表达试目录树的方 ...
- python实现的txt目录树
首先,我先表述一下我的需求: 我记笔记比较乱,但我比较容易"半途而废".文件夹很多,但大都只有一两个文件.... 所以我需要一种方式,能在不逐个打开文件夹的前提下,"看到 ...
- 第十五节:Expression表达式目录树(与委托的区别、自行拼接、总结几类实例间的拷贝)
一. 基本介绍 回忆: 最早接触到表达式目录树(Expression)可能要追溯到几年前使用EF早期的时候,发现where方法里的参数是Expression<Func<T,bool> ...
随机推荐
- Elasticsearch(ES) 下载&安装
欢迎关注笔者的公众号: 小哈学Java, 每日推送 Java 领域干货文章,关注即免费无套路附送 100G 海量学习.面试资源哟!! 个人网站: https://www.exception.site/ ...
- WebApi接口安全性 接口权限调用、参数防篡改防止恶意调用
背景介绍 最近使用WebApi开发一套对外接口,主要是数据的外送以及结果回传,接口没什么难度,采用WebApi+EF的架构简单创建一个模板工程,使用template生成一套WebApi接口,去掉put ...
- "初识".Net Winfom
对于“初识”Winform中 初识这两个字的涵义,实际上之前我一直接触的是B/S方面的知识和开发,虽然说不上是熟练,但是大部分时间都是花在B/S上了,例如MVC,如今要从B/S转到C/S了,说实话心里 ...
- [笔记] vs code 设置终端
设置文件: setting.json 1 设置自定义终端 cmd "terminal.integrated.shell.windows": "C:\\WINDOWS\\S ...
- python numba讲解
目录 一:什么是numba 二:如何使用numba 由于python有动态解释性语言的特性,跑起代码来相比java.c++要慢很多,尤其在做科学计算的时候,十亿百亿级别的运算,让python的这种 ...
- 纯C语言实现循环双向链表创建,插入和删除
#include <stdio.h> #include <stdlib.h> typedef int ElemType; typedef struct DLNode{ Elem ...
- 有两个CIDR地址块208.128/11和208.130.28/22。是否有那一个地址块包含了另一个地址?如果有,请指出,并说明理由。
有两个CIDR地址块208.128/11和208.130.28/22.是否有那一个地址块包含了另一个地址?如果有,请指出,并说明理由. 208.128/11的前缀为:11010000 100: 208 ...
- hibernate createSQLQuery StringIndexOutOfBoundsException: String index out of range: 0
有一个sql用union拼接的如下: select id,(**还有很多字段**),'' as NewName from tb1 union select id,(**还有很多字段**),name a ...
- 4 CVE-2012-0158 漏洞分析
操作系统:Windows7 32位 专业版 Office:2003sp3_20120218.exe 工具:OD和IDA 1.漏洞的本质:程序编写时未对内存拷贝函数的长度参数进行足够严谨的验证,造成的堆 ...
- odoo10学习笔记十二:web controller
转载请注明原文地址:https://www.cnblogs.com/ygj0930/p/11189332.html 一:路由 odoo.http.route(route=None, **kw) 装饰器 ...