当前位置:网站首页>C recursively obtains all files under the folder and binds them to the treeview control
C recursively obtains all files under the folder and binds them to the treeview control
2022-07-25 00:29:00 【solar storm】
C# Recursively get all the files under the folder And bind to TreeView Control
introduction :C# Recursively get all the files under the folder And bind to TreeView Control code snippet
One 、 code snippet
- 1、 The front-end code :
<TreeView x:Name="BookMarkTree" SelectedItemChanged="BookMarkTree_SelectedItemChanged" BorderThickness="2" >
<TreeView.ItemTemplate>
<HierarchicalDataTemplate ItemsSource="{Binding Children}">
<StackPanel Orientation="Vertical">
<TextBlock Text="{Binding Name}"></TextBlock>
</StackPanel>
</HierarchicalDataTemplate>
</TreeView.ItemTemplate>
</TreeView>
- 2、 Back end code
// Data node to be bound
public class Node
{
public string Name {
get; set; }
public List<Node> Children {
set; get; }
public Node()
{
Children = new List<Node>();
}
}
// Recursively get files and generate nodes
public Node GetChildren(DirectoryInfo directoryInfo)
{
Node node = new Node() {
Name = directoryInfo.Name };
var subDirectory = directoryInfo.GetDirectories();
var files = directoryInfo.GetFiles();
if(files.Length !=0)
{
foreach (var item in files)
{
// Get the file name , Remove suffix
node.Children.Add(new Node {
Name = item.Name.Replace(".html","")});
}
}
if (subDirectory.Length != 0)
{
foreach (var item in subDirectory)
{
// Continuous iteration
node.Children.Add(GetChildren(item));
}
}
return node;
}
// binding treeview Select response
private void BookMarkTree_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
{
TreeView treeView = (TreeView)sender;
Node item = (Node)treeView.SelectedItem;
// The following can be true of item To operate
}
Two 、 Use cases
// Access to the node
DirectoryInfo directoryInfo = new DirectoryInfo("./Documents/Help/Pages");
List<Node> nodes = new List<Node>();
nodes.Add(GetChildren(directoryInfo));
// by TreeView binding ItemsSource
BookMarkTree.ItemsSource = nodes;
3、 ... and 、 test result
nodes Data view :

Software screenshot :

边栏推荐
- Why do I have to clean up data?
- LeetCode_392_判断子序列
- LeetCode_6124_第一个出现两次的字母
- 2. Load test
- 数组中只出现一次的两个数字
- 采坑记录:TypeError: 'module' object is not callable
- 阿里 Seata 新版本终于解决了 TCC 模式的幂等、悬挂和空回滚问题
- C language force buckle the flipped number of question 7. Violence Act
- Improve static loading dynamic loading
- [Bert] transformer/bert/attention interview questions and answers
猜你喜欢

NXP i.mx6q development board software and hardware are all open source, and the schematic diagram of the core board is provided

Install software on kubernetes cluster using helm 3 package manager

UART

The font changes with the change of the form
![[Bert] QA, reading comprehension, information retrieval](/img/3b/f632271be813cd71a44b204f5060c4.png)
[Bert] QA, reading comprehension, information retrieval
![[Bert] transformer/bert/attention interview questions and answers](/img/32/5d29ce8056df16211630c3384adcf4.png)
[Bert] transformer/bert/attention interview questions and answers

Vscode installation and configuration

Detailed explanation of alexnet of paddlepaddle paper series (with source code)

2022 Henan Mengxin League game 2: Henan University of technology K - Rice

Dynamic programming-01 knapsack rolling array optimization
随机推荐
Qt项目-安防监控系统(各个界面功能实现)
GUI basic application
50 places are limited to open | with the news of oceanbase's annual press conference coming!
痛并快乐的-NIO编程
Leetcode 1260. two dimensional grid migration: two solutions (k simulations / one step)
Exception, import package and file operation
2022 Henan Mengxin League game 2: Henan University of technology K - Rice
Redis6.2 SYSTEMd startup prompt redis service: Failed with result ‘protocol‘.
If real-time intersection with line segments in online CAD drawings is realized
[untitled]
Improve static loading dynamic loading
Advanced function of postman
在混合云中管理数据库:八个关键注意事项
Use es to realize fuzzy search and search recommendation of personal blog
LeetCode_ 6124_ The first letter that appears twice
Usage of atomicinteger (counter)
Paper time review MB2: build a behavior model for autonomous databases
Mobile terminal touch event
The new version of SSM video tutorial in shangsilicon valley was released
First experience of flask