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. 如何选择正确的angular2学习曲线?

    参考: https://www.zhihu.com/question/50800464/answer/122921043 https://www.zhihu.com/question/48670501 ...

  2. CSS3:边框属性

    前言 学习这些CSS属性并不是要自己要设计多好看的样式,而是看到网上的代码能看得懂. 效果 本章将围绕如下效果进行解释: border border-width: 边框宽度. border-style ...

  3. linux入门总结

    linux的核心概念知识:     linux软件是开源免费的,而linux是由Unix演变而成,Unix是由MINIX演变而成. 2000年以后,linux系统日趋成熟,涌现大量基于linux服务平 ...

  4. hack games

    记下,有时间玩玩~ wargame http://www.wechall.net/lang_ranking/en --------------- Monyer系列(黑客游戏) 1. http://mo ...

  5. 转:HDFS运行原理

    简介 HDFS(Hadoop Distributed File System )Hadoop分布式文件系统.是根据google发表的论文翻版的.论文为GFS(Google File System)Go ...

  6. RMAN中format的参数

    (转自:http://blog.chinaunix.net/uid-23079711-id-2554290.html) format 的替换变量,注意大小写! 1.     %d --数据库的db_n ...

  7. session、cookie、viewstate

    session的用法 定义:保存在服务器内存的数据,sesson 只应该应用在需要跨页面且与每个访问用户相关的变量和对象存储上,session在默认情况下20分钟就过期,在页面之中最好不要过多使用,因 ...

  8. fegin 调用源码分析

    http://techblog.ppdai.com/2018/05/28/20180528/ 这篇文章是相当详细

  9. linux basename学习

    basename 用法 basename 名称 [后缀]   例子 1. $: basename /tmp/test.sh 输出: test.sh 2. $: basename /tmp/test.s ...

  10. Flask-WTF表单

    Web表单 Web 表单是 Web 应用程序的基本功能. 它是HTML页面中负责数据采集的部件.表单有三个部分组成:表单标签.表单域.表单按钮.表单允许用户输入数据,负责HTML页面数据采集,通过表单 ...