zoukankan      html  css  js  c++  java
  • Wpf TreeView demo

    xaml:

    <Window x:Class="TreeViewDemo.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            Title="MainWindow" Height="550" Width="825"
            xmlns:local="clr-namespace:TreeViewDemo">
        <Window.Resources>
            <HierarchicalDataTemplate DataType="{x:Type local:Folder}" ItemsSource="{Binding SubFolders}">
                <TextBlock Text="{Binding Name}"/>
            </HierarchicalDataTemplate>
        </Window.Resources>
        <Grid>
            <TreeView ItemsSource="{x:Static local:Data.Folders}" />
        </Grid>
    </Window>

    cs:

    namespace TreeViewDemo
    {
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
    public MainWindow()
    {
    InitializeComponent();
    }
    }

    public static class Data
    {
    private const string RootFolder = @"F:\";

    public static IEnumerable<Folder> Folders
    {
    get
    {
    return new List<Folder>()
    {
    new Folder()
    {
    Name = RootFolder,
    SubFolders = EnumerateDirectories(RootFolder)
    }
    };
    }
    }

    private static IEnumerable<Folder> EnumerateDirectories(string directory)
    {
    var folders = new List<Folder>();
    try
    {
    foreach (var dir in Directory.EnumerateDirectories(directory))
    {
    var folder = new Folder() { Name = new DirectoryInfo(dir).Name, SubFolders = EnumerateDirectories(dir) };
    folders.Add(folder);
    }

    return folders;
    }
    catch (UnauthorizedAccessException)
    {
    return null;
    }
    }
    }

    public class Folder
    {
    public IEnumerable<Folder> SubFolders { get; set; }
    public string Name { get; set; }
    }
    }

     source code can be downloaded at: https://files.cnblogs.com/bear831204/TreeViewDemo.zip

  • 相关阅读:
    MS SQL 事物日志传送能否跨数据库版本吗?
    MS SQL 模仿ORACLE的DESC
    Rhel-Server 5.5 安装ORACLE10
    ORACLE约束总结
    Win2003 设置远程连接限制数
    javascript学习代码-判断闰年
    javascript学习代码--点击按钮显示内容
    javascript学习代码
    反馈表样式
    调查表样式设计
  • 原文地址:https://www.cnblogs.com/bear831204/p/2320470.html
Copyright © 2011-2022 走看看