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 最显著的特点就是它完全由布局组件和控件 ...
随机推荐
- 构建hadoop集群时遇到的问题
在构建hadoop集群时,出现过主节点中的namenode或datanode启动不成功的问题.在日志文件中往往会显示namenode和datanode中clusterID不相同的问题,这个问题往往都是 ...
- cookie与session(略谈)
cookie (储存在用户本地终端上的数据) Cookie,有时也用其复数形式 Cookies,指某些网站为了辨别用户身份.进行 session 跟踪而储存在用户本地终端上的数据(通常经过加密).定义 ...
- Java截取图片的一部分并保存为40*40的图片
@Test public void testImag() { try { String path = "E:/flower2.jpg"; int x = 11, y = 20, c ...
- Centos7 使用Dockerfile 制作自己的Dotnetcore程序镜像
准备Centos7环境及Docker环境 从Docker hub拉取 Microsoft/dotnet 基础镜像(可以使用国内加速) 向Centos7指定目录上传Dotnet Core程序,目录: / ...
- Algorithm1: 全排列
全排列 思想: 这是一个全排列问题,需要使用递归实现,将数组中的所有元素和第一个元素交换,求后面n-1个元素的全排列. 按照这个条件递归下去,知道元素的个数只有一个的时候,输出所有 ...
- C++多线程2.beginthread
C++ 多线程2 beginthread 启动线程知识 20131021 Reference: http://blog.csdn.net/laoyang360/article/details/7720 ...
- 破解google翻译API全过程
◆版权声明:本文出自胖喵~的博客,转载必须注明出处. 转载请注明出处:http://www.cnblogs.com/by-dream/p/6554340.html 前言 google的翻译不得不承认它 ...
- iOS笔记之常用工具
CocoaPods: 类库管理工具,使用教程见http://www.devtang.com/blog/2014/05/25/use-cocoapod-to-manage-ios-lib-depende ...
- redis安装配置记录
环境:CentOS7,最小化安装 安装gcc wget # yum upgrade # yum install gcc # yum install wget 下载并安装redis # wget htt ...
- C++面向对象高级编程(八)模板
技术在于交流.沟通,转载请注明出处并保持作品的完整性. 这节课主要讲模板的使用,之前我们谈到过函数模板与类模板 (C++面向对象高级编程(四)基础篇)这里不再说明 1.成员模板 成员模板:参数为tem ...