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的更多相关文章

  1. WPF Tree多级绑定

    <Window x:Class="TreeTest.MainWindow" xmlns="http://schemas.microsoft.com/winfx/20 ...

  2. C# 中那些常用的工具类(Utility Class)(二)

    今天按照这一年来经常用到的那些静态的工具类再来做一次总结,这些小的工具来可以作为自己学习的很好的例子,通过总结这些东西,能够很大程度上梳理自己的知识体系,当然这个是经常用到的,接下来就一个个去分析这些 ...

  3. Visual Studio的Debugger Visualizers

    在英文网站上找到一份清单,列出了Visual Studio的Debugger Visualizers,觉得很好,记下来备注并分享: ASP, WEB:ASP.NET control graph vis ...

  4. 2000条你应知的WPF小姿势 基础篇<45-50 Visual Tree&Logic Tree 附带两个小工具>

    在正文开始之前需要介绍一个人:Sean Sexton. 来自明尼苏达双城的软件工程师.最为出色的是他维护了两个博客:2,000Things You Should Know About C# 和 2,0 ...

  5. WPF中的Visual Tree和Logical Tree与路由事件

    1.Visual Tree和Logical TreeLogical Tree:逻辑树,WPF中用户界面有一个对象树构建而成,这棵树叫做逻辑树,元素的声明分层结构形成了所谓的逻辑树!!Visual Tr ...

  6. WPF知识点全攻略06- WPF逻辑树(Logical Tree)和可视树(Visual Tree)

    介绍概念之前,先来分析一段代码: xaml代码如下: <Window x:Class="WpfApp1.MainWindow" xmlns="http://sche ...

  7. DevExpress WPF v19.1新版亮点:主题/Tree List等控件新功能

    行业领先的.NET界面控件DevExpress 日前正式发布v19.1版本,本站将以连载的形式介绍各版本新增内容.在本系列文章中将为大家介绍DevExpress WPF v19.1中新增的一些控件及部 ...

  8. DevExpress WPF v19.1:Data Grid/Tree List等控件功能增强

    行业领先的.NET界面控件DevExpress 日前正式发布v19.1版本,本站将以连载的形式介绍各版本新增内容.在本系列文章中将为大家介绍DevExpress WPF v19.1中新增的一些控件及部 ...

  9. WPF 中的逻辑树(Logical Tree)与可视化元素树(Visual Tree)

    一.前言 ​ WPF 中有两种"树":逻辑树(Logical Tree)和可视化元素树(Visual Tree). Logical Tree 最显著的特点就是它完全由布局组件和控件 ...

随机推荐

  1. Spark 基于物品的协同过滤算法实现

    J由于 Spark MLlib 中协同过滤算法只提供了基于模型的协同过滤算法,在网上也没有找到有很好的实现,所以尝试自己实现基于物品的协同过滤算法(使用余弦相似度距离) 算法介绍 基于物品的协同过滤算 ...

  2. python:使用Fabric自动化你的任务

    http://www.th7.cn/Program/Python/2012/03/05/62236.shtml

  3. 20165332 2017-2018-2《Java程序设计》课程总结

    20165332 2017-2018-2<Java程序设计>课程总结 一.每周作业及实验报告链接汇总 我期望的师生关系 学习基础和c语言基础调查 Linux安装及命令入门 第一周学习总结 ...

  4. oracle11g客户端如何完全卸载(转)

    1.停用Oracle服务:进入计算机管理,在服务中,找到oracle开头的所有服务,右击选择停止 2.在开始菜单中,找到Universal Installer,运行Oracle Universal I ...

  5. JavaScript--跨域

    跨域 什么是跨域? 跨域请求就是不同域的网站之间的文件数据之间的传送 ,由于浏览器的同源策略机制(基于安全,同源策略阻止从一个源加载的文档或脚本获取或设置另一个源加载的文档的属性)Ajax直接请求普通 ...

  6. c#中事务及回滚

    程序一般在特殊数据的时候,会有数据上的同步,这个时候就用到了事物.闲话不多说,直接上代码. public void UpdateContactTableByDataSet(DataSet ds, st ...

  7. 剑指offer--50.滑动窗口的最大值

    时间限制:1秒 空间限制:32768K 热度指数:157641 题目描述 给定一个数组和滑动窗口的大小,找出所有滑动窗口里数值的最大值.例如,如果输入数组{2,3,4,2,6,2,5,1}及滑动窗口的 ...

  8. DR模式下的高可用的LVS(LVS+keepalived)

    一.keepalived 在DR模式下,使用Keepalived实现LVS的高可用.Keepalived的作用是检测服务器的状态,如果有一台web服务器 宕机,或工作出现故障,Keepalived将检 ...

  9. zookeeper数据一致性与paxos算法

    数据一致性与paxos算法 据说Paxos算法的难理解与算法的知名度一样令人敬仰,所以我们先看如何保持数据的一致性,这里有个原则就是: 在一个分布式数据库系统中,如果各节点的初始状态一致,每个节点都执 ...

  10. 数据仓库(Data Warehouse)建设

    数据仓库初体验 数据库仓库架构以前弄的很简单:将各种源的数据统一汇聚到DW中,DW没有设计,只是将所有数据汇聚起来: ETL也很简单,只是将数据同步到DW中,只是遇到BUG时,处理一些错误数据,例如: ...