当前位置: 首页 > news >正文

C# Avalonia 19- DataBinding- DirectoryTreeView

DirectoryTreeView.axaml代码

<Window xmlns="https://github.com/avaloniaui"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:d="http://schemas.microsoft.com/expression/blend/2008"xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"Height="300" Width="300"x:Class="AvaloniaUI.DirectoryTreeView"Title="DirectoryTreeView"><Grid Margin="3"><TreeView Name="treeFileSystem"/></Grid>
</Window>

DirectoryTreeView.axaml.cs代码

using Avalonia;
using Avalonia.Controls;
using Avalonia.Interactivity;
using System;
using System.Collections.Generic;
using System.IO;namespace AvaloniaUI;public partial class DirectoryTreeView : Window
{private static readonly object Placeholder = new();public DirectoryTreeView(){InitializeComponent();BuildTree();}private void BuildTree(){treeFileSystem.Items.Clear();foreach (var drive in DriveInfo.GetDrives()){if (!drive.IsReady)continue;var item = CreateItem(drive.Name, drive);AddPlaceholder(item);treeFileSystem.Items.Add(item);}}private TreeViewItem CreateItem(string header, object tag){var item = new TreeViewItem{Header = header,Tag = tag};item.Expanded += ItemExpanded;return item;}private void ItemExpanded(object? sender, RoutedEventArgs e){if (sender is not TreeViewItem item)return;// 只在第一次展开(且存在占位符)时加载if (item.Items.Count != 1 || !ReferenceEquals(item.Items[0], Placeholder))return;item.Items.Clear();var dir = GetDirectoryInfo(item.Tag);if (dir is null)return;foreach (var subDir in SafeEnumerateDirectories(dir)){var child = CreateItem(subDir.Name, subDir);// 只有确实还有子目录,才显示展开箭头(放占位符)if (HasSubdirectories(subDir))AddPlaceholder(child);item.Items.Add(child);}}private static DirectoryInfo? GetDirectoryInfo(object? tag){if (tag is DriveInfo drive)return drive.RootDirectory;if (tag is DirectoryInfo dir)return dir;return null;}private static void AddPlaceholder(TreeViewItem item){item.Items.Add(Placeholder);}private static IEnumerable<DirectoryInfo> SafeEnumerateDirectories(DirectoryInfo dir){IEnumerator<DirectoryInfo>? enumerator = null;try{enumerator = dir.EnumerateDirectories().GetEnumerator();}catch{yield break;}using (enumerator as IDisposable){while (true){DirectoryInfo current;try{if (!enumerator.MoveNext())break;current = enumerator.Current;}catch{// 某个子目录读不了:跳过,不影响整层continue;}yield return current;}}}private static bool HasSubdirectories(DirectoryInfo dir){try{using var e = dir.EnumerateDirectories().GetEnumerator();return e.MoveNext();}catch{return false;}}
}

运行效果

image

 

http://www.jsqmd.com/news/391729/

相关文章:

  • 2026信用卡逾期不用慌!债务协商全国优质处理律所+口碑协商机构口碑榜,负债人上岸指南 - 代码非世界
  • 天虹购物卡回收攻略:如何选择靠谱平台并掌握回收流程 - 团团收购物卡回收
  • 夜间无人自主作业,输入作业区域,任务,处理,夜间定位+避障,输出,自动完成面积,时间。
  • 毕业论文神器 10个AI论文软件深度测评:本科生高效写作与格式规范全攻略
  • 从此告别拖延,一键生成论文工具,千笔·专业学术智能体 VS Checkjie
  • 2026香港租车指南:商务服务优,口碑传四方,跨境包车/跨境租车/企业租车/大巴租赁/租赁,租车企业推荐排行榜单 - 品牌推荐师
  • OSS生命周期管理与通过CloudLens for OSS接入目标Bucket进行监控统计
  • 原“双一流”副校长,任省会大学校长
  • 2026年蘑菇石厂家排行,品质与口碑并存,贴墙石/冰裂纹/地铺石/文化石/天然石/脚踏石/石材,蘑菇石品牌推荐榜单 - 品牌推荐师
  • 用数据说话 9个一键生成论文工具测评:研究生毕业论文+科研写作必备神器
  • 生成引擎优化(GEO)在提升内容创作效率与用户参与中的创新应用解析
  • 深入解析:在线绘制特殊形状(三角行,菱形,五边形,六边形,椭圆,圆形)聚类热图
  • 刚刚,DeepSeek V4基准测试泄露!全场惊呼新王归来
  • OpenClaw#x2B;OpenViking #x2B; NVIDIA API 配置教程
  • 2026年比较好的国标安全带/防坠落安全带高评价直销厂家采购指南推荐(高评价) - 行业平台推荐
  • ICLR 2026 | 陈宝权团队提出FieryGS:首次让AI生成真实火焰
  • 2026年医药人才招聘与薪酬白皮书
  • 2026年质量好的展示柜珠宝柜滑轨/简约珠宝柜滑轨精选供应商推荐口碑排行 - 行业平台推荐
  • 一文搞懂【STM32G4-FOC】(5)DAC 闭环输出链路:基于同步采样的幅值与频率调制:核心原理+实战案例
  • 2026别错过!备受喜爱的降AIGC平台 —— 千笔·专业降AI率智能体
  • 鲜牛肉采购必看:当前市场供应商特点分析,白牦牛肉/新鲜牛肉/牛肉/白牦牛/鲜牛肉/天祝白牦牛肉,鲜牛肉供应商哪个好 - 品牌推荐师
  • 随机需求下的库存优化:报童模型与安全库存实操
  • 2026年比较好的304过滤网板/不锈钢网状过滤网板供应商采购指南怎么联系 - 行业平台推荐
  • 2026年知名的反弹加缓冲防摆动滑轨/防摆动滑轨源头厂家推荐帮我推荐几家 - 行业平台推荐
  • 大年初一,百度智能云携硅基「朋友圈」给您AI云拜年!
  • 深度解读:分期乐购物额度的使用范围和回收流程 - 团团收购物卡回收
  • 基于YOLOv8的无人机行人目标检测项目|完整源码数据集+PyQt5界面+完整训练流程+开箱即用!
  • 山东一卡通的使用与回收:这几个注意事项你必须知道 - 团团收购物卡回收
  • 回收中石化加油卡前必读清单 - 京顺回收
  • 2026年靠谱的反弹上翻门/液压上翻门生产商实力参考哪家质量好(更新) - 行业平台推荐