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 最显著的特点就是它完全由布局组件和控件 ...
随机推荐
- 毕业设计总结(1)-canvas画图
去年6月底完成的毕业设计,到现在也才开始给它做个总结,里面有很多可以学习和借鉴的东西. 我的毕业设计的题目是“一种路径规划算法的改进与设计”,具体的要求可参见下面的表格: 题目 一种路径规划算法的改进 ...
- PHP XML Parser 函数
PHP XML Parser 简介 XML 函数允许您解析 XML 文档,但无法对其进行验证. XML 是一种用于标准结构化文档交换的数据格式.您可以在我们的 XML 教程 中找到更多有关 XML 的 ...
- jQuery 获取Select选择的Text和 Value
jQuery获取Select选择的Text和Value:语法解释: 1. $("#select_id").change(function(){//code...}); / ...
- Python连接SQLite数据库代码
import sqlite3 # create database conn = sqlite3.connect('test.db') #不存在就创建后再打开 print ("Opened d ...
- C语言实现顺序表
C语言实现顺序表代码 文件SeqList.cpp #pragma warning(disable: 4715) #include"SeqList.h" void ShowSeqLi ...
- Linux命令详解-cp
cp 命令用来复制文件或者目录,是Linux系统中最常用的命令之一.一般情况下,shell会设置一个别名,在命令行下复制文件时,如果目标文件已经存在, 就会询问是否覆盖,不管你是否使用-i参数.但是如 ...
- 使用python编写微信跳一跳的自动脚本
实现思路: 调用adb命令,截图 寻找小小人的底部中心点role(从下到上扫描,直到找到小小人相同像素的点,至于小小人像素点rgb是什么,可以使用photoshop查看) 寻找棋盘最高点top,然后寻 ...
- MongoDB.Driver 2.4以上版本 在.NET中的基本操作
MongoDB.Driver是操作mongo数据库的驱动,最近2.0以下版本已经从GitHub和Nuget中移除了,也就是说.NET Framework4.0不再能从官方获取到MongoDB的驱动了, ...
- UVA-1312 Cricket Field (技巧枚举)
题目大意:在一个w*h的网格中,有n个点,找出一个最大的正方形,使得正方形内部没有点. 题目分析:寻找正方形实质上等同于寻找矩形(只需令长宽同取较短的边长).那么枚举出所有可能的长宽组合取最优答案即可 ...
- 在Linux下安装JDK环境
解压java安装包到指定目录下,这里为/data/software/java/, 然后在/etc/profile添加以下内容: JAVA_HOME =/data/software/java/jdk1. ...