wpf Tree
code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes; namespace CodeAI
{
public class Node
{
public Node()
{
this.Nodes = new List<Node>();
this.ParentID = -;
}
public int ID { get; set; }
public string Name { get; set; }
public int ParentID { get; set; }
public List<Node> Nodes { get; set; }
} /// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent(); new TemplateControl().load(); List<Node> nodes = new List<Node>()
{
new Node { ID = , Name = "中国" },
new Node { ID = , Name = "北京市", ParentID = },
new Node { ID = , Name = "吉林省", ParentID = },
new Node { ID = , Name = "上海市", ParentID = },
new Node { ID = , Name = "海淀区", ParentID = },
new Node { ID = , Name = "朝阳区", ParentID = },
new Node { ID = , Name = "大兴区", ParentID = },
new Node { ID = , Name = "白山市", ParentID = },
new Node { ID = , Name = "长春市", ParentID = },
new Node { ID = , Name = "抚松县", ParentID = },
new Node { ID = , Name = "靖宇县", ParentID = }
};
List<Node> outputList = Bind(nodes); this.TreeView.ItemsSource = outputList;
}
List<Node> Bind(List<Node> nodes)
{
List<Node> outputList = new List<Node>();
for (int i = ; i < nodes.Count; i++)
{
if (nodes[i].ParentID == -) outputList.Add(nodes[i]);
else FindDownward(nodes, nodes[i].ParentID).Nodes.Add(nodes[i]);
}
return outputList;
} Node FindDownward(List<Node> nodes, int id)
{
if (nodes == null) return null;
for (int i = ; 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 TextBlock_MouseDown_1(object sender, MouseButtonEventArgs e)
{ }
}
}
xaml
<Window x:Class="CodeAI.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:src="clr-namespace:CodeAI"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Grid.Resources>
<HierarchicalDataTemplate DataType="{x:Type src: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="/WpfTest;Component/Resources/KnowDot.png" Width="16" Height="16" />-->
<TextBlock Text="{Binding Name}" ToolTip="{Binding Name}" Tag="{Binding}" MouseDown="TextBlock_MouseDown_1"/>
</StackPanel>
</HierarchicalDataTemplate>
</Grid.Resources>
<TreeView Name="TreeView"/> </Grid>
</Window>
树形版
code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace CodeAI
{
public class TemplateNode
{
public TemplateNode()
{
this.childs = new List<TemplateNode>();
}
public string Name { get; set; } private TemplateNode parent;
public TemplateNode Parent
{
set
{
value.childs.Add(this);
parent = value;
}
get
{
return parent;
}
}
public List<TemplateNode> childs { get; set; } public void addChild(TemplateNode node)
{
node.parent = this;
childs.Add(node);
}
public string filePath; public bool PictureVisible
{
get{
if(parent != null && parent.parent == null){
return true;
}
return false;
}
}
}
}
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes; namespace CodeAI
{
/// <summary>
/// TemplateControl.xaml 的交互逻辑
/// </summary>
public partial class TemplateControl : UserControl
{
public TemplateControl()
{
InitializeComponent();
} public void load()
{
TemplateNode root = new TemplateNode();
root.Name = "root";
if(Directory.Exists("template"))
{
loadSub("template", root);
string[] subFolder = Directory.GetDirectories("template");
}
} public void loadSub(string folderPath, TemplateNode parent)
{
string[] subFolders = Directory.GetDirectories(folderPath); foreach (string filePath in Directory.GetFiles(folderPath))
{
TemplateNode node = new TemplateNode();
node.filePath = filePath;
node.Name = System.IO.Path.GetFileName(filePath);
node.Parent = parent;
} foreach (string subFolder in subFolders)
{
TemplateNode subParent = new TemplateNode();
subParent.filePath = subFolder;
subParent.Name = System.IO.Path.GetDirectoryName(subFolder);
subParent.Parent = parent; loadSub(folderPath , subParent);
}
}
}
}
xaml
<UserControl x:Class="CodeAI.TemplateControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
xmlns:src="clr-namespace:CodeAI"
d:DesignHeight="300" d:DesignWidth="300">
<UserControl.Resources>
<HierarchicalDataTemplate DataType="{x:Type src:TemplateNode}" ItemsSource="{Binding childs}">
<StackPanel Orientation="Horizontal" Margin="0,2,0,2">
<Image Source="image/folder.png" Visibility="{Binding PictureVisible}" Width="16" Height="16" />
<TextBlock Text="{Binding Name}" ToolTip="{Binding Name}" Tag="{Binding}" />
</StackPanel>
</HierarchicalDataTemplate>
</UserControl.Resources> <Grid>
<TreeView Name="TreeView"/>
</Grid>
</UserControl>
节点事件
使用模板HierarchicalDataTemplate

<HierarchicalDataTemplate x:Key="BookMarkTemplate" >
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Child.MarkName,Mode=TwoWay}"></TextBlock>
</StackPanel>
</HierarchicalDataTemplate>
<HierarchicalDataTemplate x:Key="ChapterTemplate" ItemTemplate="{StaticResource BookMarkTemplate}" ItemsSource="{Binding InlineList,Mode=TwoWay}" >
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Name}"></TextBlock>
</StackPanel>
</HierarchicalDataTemplate>

从父节点删除选中项
<TreeView x:Name="treeview" TreeViewItem.Selected="treeView1_Selected" ItemTemplate="{StaticResource ChapterTemplate}">

private void treeView1_Selected(object sender, RoutedEventArgs e)
{
if ((e.OriginalSource as TreeViewItem).Header.GetType()==typeof(InlineUIContainer))
{
mark = ((e.OriginalSource as TreeViewItem).Header as InlineUIContainer);
BookMarkRun run = mark.Child as BookMarkRun;
txtSelectionContent.Text = run.MarkName;
DependencyObject parent = VisualTreeHelper.GetParent((e.OriginalSource as TreeViewItem));
while (!(parent is TreeViewItem))
parent = VisualTreeHelper.GetParent(parent);
TreeViewItem item = (TreeViewItem)parent;
volumeModel = (item.Header as VolumeModel);
btnAdd.Content = "修改";
btnDel.IsEnabled = true;
btnAdd.IsEnabled = true;
}
}

wpf Tree的更多相关文章
- WPF Tree多级绑定
<Window x:Class="TreeTest.MainWindow" xmlns="http://schemas.microsoft.com/winfx/20 ...
- C# 中那些常用的工具类(Utility Class)(二)
今天按照这一年来经常用到的那些静态的工具类再来做一次总结,这些小的工具来可以作为自己学习的很好的例子,通过总结这些东西,能够很大程度上梳理自己的知识体系,当然这个是经常用到的,接下来就一个个去分析这些 ...
- Visual Studio的Debugger Visualizers
在英文网站上找到一份清单,列出了Visual Studio的Debugger Visualizers,觉得很好,记下来备注并分享: ASP, WEB:ASP.NET control graph vis ...
- 2000条你应知的WPF小姿势 基础篇<45-50 Visual Tree&Logic Tree 附带两个小工具>
在正文开始之前需要介绍一个人:Sean Sexton. 来自明尼苏达双城的软件工程师.最为出色的是他维护了两个博客:2,000Things You Should Know About C# 和 2,0 ...
- WPF中的Visual Tree和Logical Tree与路由事件
1.Visual Tree和Logical TreeLogical Tree:逻辑树,WPF中用户界面有一个对象树构建而成,这棵树叫做逻辑树,元素的声明分层结构形成了所谓的逻辑树!!Visual Tr ...
- WPF知识点全攻略06- WPF逻辑树(Logical Tree)和可视树(Visual Tree)
介绍概念之前,先来分析一段代码: xaml代码如下: <Window x:Class="WpfApp1.MainWindow" xmlns="http://sche ...
- DevExpress WPF v19.1新版亮点:主题/Tree List等控件新功能
行业领先的.NET界面控件DevExpress 日前正式发布v19.1版本,本站将以连载的形式介绍各版本新增内容.在本系列文章中将为大家介绍DevExpress WPF v19.1中新增的一些控件及部 ...
- DevExpress WPF v19.1:Data Grid/Tree List等控件功能增强
行业领先的.NET界面控件DevExpress 日前正式发布v19.1版本,本站将以连载的形式介绍各版本新增内容.在本系列文章中将为大家介绍DevExpress WPF v19.1中新增的一些控件及部 ...
- WPF 中的逻辑树(Logical Tree)与可视化元素树(Visual Tree)
一.前言 WPF 中有两种"树":逻辑树(Logical Tree)和可视化元素树(Visual Tree). Logical Tree 最显著的特点就是它完全由布局组件和控件 ...
随机推荐
- c中%
%u 十进制无符号整数 %f 浮点数 %s 字符串 %c 单个字符 %p 指针的值 %e 指数形式的浮点数 %x, %X 无符号以十六进制表示的整数 %0 无符号以八进制表示的整数 %g 自动选择合适 ...
- Shell date 命令详解
格式: date [选项] ... [+格式] 选项说明: -d ,--date=字符串 显示指定字符串所描述的时间 格式说明: 例子1: #!/bin/bash ##. 获取当前系统时间 YYYY- ...
- 设计模式--原型模式C++实现
原型模式C++实现 1定义 用原型实例指定创建对象的种类,并且通过拷贝这些原型创建新的对象 2类图 3实现 class Prototype { protected: Prototype(); publ ...
- classpath到底指的哪里
之前一直对classpath不太明白到底指的哪里,今天研究了一下,做个总结.. classpath顾名思义就是指类路径,但是这样解释可能还是不明白,这里拿一个SpringBoot应用编译后生成的tar ...
- HDU 5699 二分+线性约束
http://acm.hdu.edu.cn/showproblem.php?pid=5699 此题满足二分性质,关键在于如何判断当前的时间值可以满足所有的运送方案中的最长的时间. 对于每一次枚举出的k ...
- 执行Maven install或Maven test命令时控制台输出乱码的解决办法
[解决方案一] 在Maven的pom.xml文件中增加如下代码: <properties> <argLine>-Dfile.encoding=UTF-8</argLine ...
- 使用wepy框架搭建微信小程序采坑记(一)
1.什么是wepy 这个框架是腾讯内部出的一个类MVVM的小程序开发框架.大体上来说语法是类VUE的,所以如果有VUE开发经验的话迁移成本会低一些.至于具体的怎么使用我就不赘言了,有问题查文档(官方文 ...
- Django开发BUG "Model class WH_auth.models.User doesn't declare an explicit app_label and isn't in an application in INSTALLED_APPS."
当进行数据库迁移的时候发生问题,报错如下:RuntimeError: Model class WH_auth.models.User doesn't declare an explicit app_l ...
- 通过一道面试题了解Condition线程通信
Condition Condition接口描述了可能会与锁有关联的条件变量.这些变量在用法与使用Object.wait访问的隐式监视器类似,但提供了更强大的功能.需要特别指出的是,单个Lock可能与多 ...
- 019——VUE中v-for与computer结合功能实例讲解
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...