定义树节点,(编译环境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 目录树绑定 与 遍历的更多相关文章

  1. WPF 4 目录树型显示

    原文:WPF 4 目录树型显示      本篇将通过WPF4 制作简单的目录树型结构显示实例,完成本篇内容我们将作出下图所示的应用程序.      从图中我们可以看到程序主要分为两部分:左边显示本地驱 ...

  2. python GUI编程tkinter示例之目录树遍历工具

    摘录 python核心编程 本节我们将展示一个中级的tkinter应用实例,这个应用是一个目录树遍历工具:它会从当前目录开始,提供一个文件列表,双击列表中任意的其他目录,就会使得工具切换到新目录中,用 ...

  3. 遍历目录树 - Unicode 模式

    =info     遍历目录树 支持 Unicode     Code by 523066680@163.com     2017-03         V0.5 使用Win32API判断目录硬链接 ...

  4. 利用树的先序和后序遍历打印 os 中的目录树

    [0]README 0.1)本代码均为原创,旨在将树的遍历应用一下下以加深印象而已:(回答了学习树的遍历到底有什么用的问题?)你对比下linux 中的文件树 和我的打印结果就明理了: 0.2)我们采用 ...

  5. Android原生PDF功能实现:PDF阅读、PDF页面跳转、PDF手势伸缩、PDF目录树、PDF预览缩略图

    1.背景 近期,公司希望实现安卓原生端的PDF功能,要求:高效.实用. 经过两天的调研.编码,实现了一个简单Demo,如上图所示. 关于安卓原生端的PDF功能实现,技术点还是很多的,为了咱们安卓开发的 ...

  6. 05.表达式目录树Expression

    参考文章 https://www.cnblogs.com/xyh9039/p/12748983.html 1. 基本了解 1.1 Lambda表达式 演变过程 using System; namesp ...

  7. C#表达式目录树(Expression)

    1.什么是表达式目录树 :简单的说是一种语法树,或者说是一种数据结构(Expression) 2.用Lambda声明表达式目录树: Expression<Func<; //表达试目录树的方 ...

  8. python实现的txt目录树

    首先,我先表述一下我的需求: 我记笔记比较乱,但我比较容易"半途而废".文件夹很多,但大都只有一两个文件.... 所以我需要一种方式,能在不逐个打开文件夹的前提下,"看到 ...

  9. 第十五节:Expression表达式目录树(与委托的区别、自行拼接、总结几类实例间的拷贝)

    一. 基本介绍 回忆: 最早接触到表达式目录树(Expression)可能要追溯到几年前使用EF早期的时候,发现where方法里的参数是Expression<Func<T,bool> ...

随机推荐

  1. Docker 下开发安装hyperf

    Docker 下开发hyperf # 下载并运行 hyperf/hyperf 镜像,并将镜像内的项目目录绑定到宿主机的 /tmp/skeleton 目录 docker run -v /tmp/skel ...

  2. 如何让 FFmpeg 支持异步并行转码、截图等等操作?

    直接贴代码了: ffmpegTest02.cs public partial class ffmpegTest02 : FormBase { private static readonly strin ...

  3. SqlServer 开篇简介

    实例:我们的电脑中可以安装一个或多个SqlServer实例,每一个SqlServer实例可以包含一个或者多个数据库. 架构:数据库中,又有一个或者多个架构.架构里面包含:表,视图,存储过程. 文件与文 ...

  4. U9创建BE组件

    打开UBF,新建项目->实体项目 输入名称后,点击确定,第二步:修改名称以在后期作为文件夹区分 第三步:创建实体 第四步:添加U9基础对象引用 拖动到解决方案的Reference 第五步:右键构 ...

  5. [笔记] C# 如何获取文件的 MIME Type

    MIME Type 为何物: MIME 参考手册 svn.apache.org/repos/asf/httpd/httpd/trunk/docs/conf/mime.types 常规方式 对于有文件后 ...

  6. SpirngBoot整合Spring-data-JPA

    0.引言 使用SpringBoot data jpa技术相比mybatis是比较难的,这里只给出整合方法 1.引入SpringBoot data jpa <!--JPA依赖--> < ...

  7. odoo12 如何设置超级用户

    在odoo12的版本中,和之前的版本有点不一样的地方 在odoo12版本之前,每个实例都是使用户名为Administrator的默认用户来创建的. 在数据库中user_id是1. 在代码中,你会发现 ...

  8. vuejs的导航栏固定

    https://blog.csdn.net/wang1006008051/article/details/78003974 博主文章,超级详细,上面传送们 不过博主的导航栏跳动比较明显,我自己做了修复 ...

  9. Linux Workqueue【转】

    转自:http://kernel.meizu.com/linux-workqueue.html 21 August 2016   Workqueue 是内核里面很重要的一个机制,特别是内核驱动,一般的 ...

  10. Jenkins参数化构建(七)

    一.配置参数化构建过程 主要用来区分分支,使用传参的方式,将分支名称传入脚本中进行拉取代码. 1.1 最常用的是:字符参数.文本参数.  1.2 添加字符参数和文本参数,并配置变量名称  1.3 配置 ...