惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Forbes - Security
Forbes - Security
H
Hackread – Cybersecurity News, Data Breaches, AI and More
N
Netflix TechBlog - Medium
Engineering at Meta
Engineering at Meta
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
F
Fortinet All Blogs
P
Privacy & Cybersecurity Law Blog
The Hacker News
The Hacker News
博客园 - 司徒正美
博客园 - 聂微东
T
The Blog of Author Tim Ferriss
I
Intezer
WordPress大学
WordPress大学
Security Archives - TechRepublic
Security Archives - TechRepublic
Scott Helme
Scott Helme
T
Threat Research - Cisco Blogs
T
Tailwind CSS Blog
月光博客
月光博客
Recent Announcements
Recent Announcements
博客园 - 叶小钗
J
Java Code Geeks
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
L
LINUX DO - 热门话题
Google DeepMind News
Google DeepMind News
C
Cyber Attacks, Cyber Crime and Cyber Security
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
N
News and Events Feed by Topic
A
Arctic Wolf
B
Blog RSS Feed
Recorded Future
Recorded Future
D
DataBreaches.Net
有赞技术团队
有赞技术团队
Project Zero
Project Zero
U
Unit 42
T
Tor Project blog
The GitHub Blog
The GitHub Blog
Hacker News - Newest:
Hacker News - Newest: "LLM"
C
CERT Recently Published Vulnerability Notes
博客园 - Franky
博客园 - 【当耐特】
Microsoft Security Blog
Microsoft Security Blog
C
Cybersecurity and Infrastructure Security Agency CISA
Security Latest
Security Latest
C
Cisco Blogs
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Blog — PlanetScale
Blog — PlanetScale
H
Heimdal Security Blog
Vercel News
Vercel News
Stack Overflow Blog
Stack Overflow Blog
S
Securelist

博客园 - dalgleish

C# Avalonia 23- OpenGL- 例子通用类 C# Avalonia 22- SoundAndVideo- SpeechVoiceTest C# Avalonia 22- SoundAndVideo- SpeechRecognition C# Avalonia 22- SoundAndVideo- SpeechSynthesis C# Avalonia 22- SoundAndVideo- CodePlayback C# Avalonia 22- SoundAndVideo- SoundPlayerTest C# Avalonia 21- MenusAndToolbars- RibbonTest C# Avalonia 21- MenusAndToolbars- ProportionalStatusBar C# Avalonia 21- MenusAndToolbars- BasicToolbar C# Avalonia 21- MenusAndToolbars- ToolBar/Ribbon开源项目介绍和修复 C# Avalonia 21- MenusAndToolbars- SidebarMenu C# Avalonia 21- MenusAndToolbars- MixedMenus C# Avalonia 20 - WindowsMenu- 魔改Hyperlink - 使用例子 C# Avalonia 20 - WindowsMenu- 魔改Hyperlink C# Avalonia 20 - WindowsMenu- WebViewTest C# Avalonia 20 - WindowsMenu- WebView C# Avalonia 20 - WindowsMenu- ModernWindowTest C# Avalonia 20 - WindowsMenu- ModernWindow C# Avalonia 20 - WindowsMenu- TransparentWithShapes C# Avalonia 20 - WindowsMenu- TransparentBackground C# Avalonia 20 - WindowsMenu- OpenFileTest C# Avalonia 20 - WindowsMenu- SavePostion C# Avalonia 20 - WindowsMenu- CenterScreen C# Avalonia 19- DataBinding- DataGridGrouping C# Avalonia 19- DataBinding- BoundTreeView C# Avalonia 19- DataBinding- CustomListViewTest
C# Avalonia 19- DataBinding- DirectoryTreeView
dalgleish · 2026-02-18 · via 博客园 - dalgleish

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