<Window x:Class="TreeFileSystem.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <TreeView Name="treeFileSystem" TreeViewItem.Expanded="treeFileSystem_Expanded_1">
            
        </TreeView>
    </Grid>

</Window>

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 TreeFileSystem
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            foreach (DriveInfo drive in DriveInfo.GetDrives())
            {
                TreeViewItem item = new TreeViewItem();
                item.Tag = drive;
                item.Header = drive.ToString();

                item.Items.Add("*");
                treeFileSystem.Items.Add(item);
            }
        }

        private void treeFileSystem_Expanded_1(object sender, RoutedEventArgs e)
        {
            TreeViewItem item = (TreeViewItem)e.OriginalSource;
            item.Items.Clear();
            DirectoryInfo dir;
            if (item.Tag is DriveInfo)
            {
                DriveInfo drive = (DriveInfo)item.Tag;
                dir = drive.RootDirectory;
            }
            else
            {
                dir = (DirectoryInfo)item.Tag;
            }
            try
            {
                foreach (DirectoryInfo subDir in dir.GetDirectories())
                {
                    TreeViewItem newItem = new TreeViewItem();
                    newItem.Tag = subDir;
                    newItem.Header = subDir.ToString();
                    newItem.Items.Add("*");
                    item.Items.Add(newItem);
                }
            }
            catch
            {

            }
        }
    }
}

WPF TreeView遍历硬盘的更多相关文章

  1. WPF TreeView SelectedItemChanged called twice

    How to avoid WPF TreeView SelectedItemChanged being called twice Very often, we need to execute some ...

  2. WPF TreeView HierarchicalDataTemplate

    原文 WPF TreeView HierarchicalDataTemplate HierarchicalDataTemplate 的DataType是本层的绑定,而ItemsSource是绑定下层的 ...

  3. WPF TreeView Indent 减少节点的缩进

    www.swack.cn - 原文链接:WPF TreeView Indent 减少节点的缩进 问题 最近一个需求,需要在界面中实现Windows资源管理器TreeView的界面.但是我发现,我做出的 ...

  4. WPF TreeView 后台C#选中指定的Item, 需要遍历

               private TreeViewItem FindTreeViewItem(ItemsControl container, object item)         {      ...

  5. WPF TreeView递归遍历相关方法

    /// <summary> /// 递归改变组织树选中状态. /// </summary> /// <param name="org">< ...

  6. WPF TreeView的使用

    WPF提供了treeView控件,利用该控件开发者可以将数据分层显示在树结构中.当然其中需要用到Binding的机制,有用的类包括:ObjectDataProvider.DataTemplate.Hi ...

  7. WPF TreeView 虚拟化-设置滚动到选中项

    前言 列表滚动到具体的数据项? ListBox提供了简易快捷的滚动定位函数ScrollIntoView. TreeView树状结构列表,则没有此类方法,无法与ListBox一样,直接设置滚动到具体的数 ...

  8. 【原创】WPF TreeView带连接线样式的优化(WinFrom风格)

    一.前言 之前查找WPF相关资料的时候,发现国外网站有一个TreeView控件的样式,是WinFrom风格的,样式如下,文章链接:https://www.codeproject.com/tips/67 ...

  9. wpf TreeView

    <Window x:Class="WpfTutorialSamples.TreeView_control.TreeViewDataBindingSample"        ...

随机推荐

  1. php 字符串 去掉 html标签

    echo strip_tags("Hello <b>world!</b>");

  2. Java的面向AOP编程

    一. 引言 AOP(Aspect-Oriented Programming,面向切面的编程),是一种新型的编程范式,主张关注软件流程中的一个切面,将相同功能的代码整合打包在一起,减少系统的耦合性,增强 ...

  3. struts2基本的配置代码

    <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "- ...

  4. [Vue] Use basic event handling in Vue

    Let's use a range of events and their modifiers to look at the cool ways we can deal with event hand ...

  5. C#趣味程序---个位数为6,且能被3整出的五位数

    using System; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { int ...

  6. PHP移动互联网开发笔记(5)——基础函数库

    一.数学函数库 ● floor 舍一取整(向下取整) float floor (float $value); <?php echo(floor(0.60)."<br>&qu ...

  7. 历届图灵奖 (Turing award)得奖名单

    历届图灵奖 (Turing award)得奖名单 一.总结 一句话总结:各个方面都有. 二.历届图灵奖 (Turing award)得奖名单 Turing奖最早设立于1966年,是美国计算机协会在计算 ...

  8. js进阶 9-10 html中如何遍历下拉列表

    js进阶 9-10  html中单选框和多选框如何遍历下拉列表 一.总结 一句话总结: 1.select元素的options.length可以获取选择长度,然后用options[i]精确定位到选项,用 ...

  9. TCP协议的一些认识及实践

    http://www.2cto.com/net/201210/163047.html 一.简介 引用<TCP/IP详解-卷1>中的介绍,TCP与UDP使用相同的网络层(IP层),TCP却向 ...

  10. 小强的HTML5移动开发之路(38)——jqMobi插件ActionSheet

    现在在手机客户端上Action Sheet非常常见,比如微信中的分享按钮菜单,下面我们使用jqMobi实现一个Action Sheet,如下: 首先右击上面的按钮选择审查元素(我用的是Chrome浏览 ...