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

推荐订阅源

Security Latest
Security Latest
Know Your Adversary
Know Your Adversary
S
Schneier on Security
K
Kaspersky official blog
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Google DeepMind News
Google DeepMind News
Project Zero
Project Zero
Recorded Future
Recorded Future
T
The Blog of Author Tim Ferriss
P
Palo Alto Networks Blog
Engineering at Meta
Engineering at Meta
MyScale Blog
MyScale Blog
L
LINUX DO - 热门话题
The Register - Security
The Register - Security
B
Blog
V
Vulnerabilities – Threatpost
M
MIT News - Artificial intelligence
P
Proofpoint News Feed
Cyberwarzone
Cyberwarzone
Latest news
Latest news
Webroot Blog
Webroot Blog
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
美团技术团队
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
C
CXSECURITY Database RSS Feed - CXSecurity.com
L
LINUX DO - 最新话题
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
B
Blog RSS Feed
aimingoo的专栏
aimingoo的专栏
The GitHub Blog
The GitHub Blog
D
Docker
G
Google Developers Blog
T
Threatpost
F
Full Disclosure
Attack and Defense Labs
Attack and Defense Labs
L
Lohrmann on Cybersecurity
Application and Cybersecurity Blog
Application and Cybersecurity Blog
月光博客
月光博客
D
DataBreaches.Net
WordPress大学
WordPress大学
F
Fortinet All Blogs
S
Secure Thoughts
Y
Y Combinator Blog
博客园 - Franky
Scott Helme
Scott Helme
Apple Machine Learning Research
Apple Machine Learning Research
Hacker News: Ask HN
Hacker News: Ask HN
酷 壳 – CoolShell
酷 壳 – CoolShell
Cloudbric
Cloudbric
N
News | PayPal Newsroom

博客园 - 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