从WPF中的文件路径列表填充treeview [英] populate treeview from list of file paths in wpf

查看:347
本文介绍了从WPF中的文件路径列表填充treeview的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有几个示例,这些示例说明如何从诸如 this 或其他示例。我似乎找不到WPF这样的示例。我知道我可以集成Windows窗体并使用其他控件以使其工作,但是如果我可以使用wpf treeview控件执行相同的操作,那将是很好的。我要构造的树状视图包含大约50,000个文件,因此我认为将其绑定到某个东西会更好。但是首先,在绑定它之前,我认为基于字符串列表(字符串包含文件的路径)构造一个字符串会有所帮助。

There are several examples of how to populate a tree view from a collection of file paths such as this or this other example. I cannot seem to find such example for WPF. I know I can integrate windows forms and use a different control in order to make it work but it will be nice if I could do the same thing with a wpf treeview control. The tree view that I want to construct consists of about 50,000 files therefore I think it will be better if it is bind it to something. But first before binding it, I think it will be helpful to construct one based on a List of strings (strings contains the paths of files).

推荐答案

我对这个问题很感兴趣,并将其组合在一起。作为第一步,我想我已经很接近您的要求了。谈论50,000件物品会让我认为延迟加载可能是适当的。无论如何,这是基于乔什·史密斯的文章。我将所有代码都放在这里,但是魔术确实是由数据模板发生的。

I was intrigued by the question and threw this together. As a first pass I think I'm pretty close to what you're looking for. Talking about 50,000 items though makes me think that lazy loading may be appropriate. Anyway, here is the simple version based on an article by Josh Smith. I put all of the code here, but the magic really takes place with the data templates.

给出几个类来表示我们正在使用的对象...

Given a few classes to represent the objects we're working with...

using System.Collections.Generic;

namespace WpfTreeViewBinding.Model
{
    public class Item
    {
        public string Name { get; set; }
        public string Path { get; set; }
    }
}

和...

namespace WpfTreeViewBinding.Model
{
    public class FileItem : Item
    {

    }
}

和...

namespace WpfTreeViewBinding.Model
{
    public class DirectoryItem : Item
    {
        public List<Item> Items { get; set; }

        public DirectoryItem()
        {
            Items = new List<Item>();
        }
    }
}

我创建了一个递归方法加载一些目录/文件...

I created a recursive method to load up some directories/files...

using System.Collections.Generic;
using System.IO;
using WpfTreeViewBinding.Model;

namespace WpfTreeViewBinding
{
    public class ItemProvider
    {
        public List<Item> GetItems(string path)
        {
            var items = new List<Item>();

            var dirInfo = new DirectoryInfo(path);

            foreach(var directory in dirInfo.GetDirectories())
            {
                var item = new DirectoryItem
                               {
                                   Name = directory.Name,
                                   Path = directory.FullName,
                                   Items = GetItems(directory.FullName)
                               };

                items.Add(item);
            }

            foreach(var file in dirInfo.GetFiles())
            {
                var item = new FileItem
                               {
                                   Name = file.Name, 
                                   Path = file.FullName
                               };

                items.Add(item);
            }

            return items;
        }
    }
}

从那里开始使用System.Windows获取数据...

From there it's just a matter of getting the data...

using System.Windows;

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

            var itemProvider = new ItemProvider();

            var items = itemProvider.GetItems("C:\\Temp");

            DataContext = items;
        }
    }
}

并显示它...

<Window x:Class="WpfTreeViewBinding.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
        xmlns:Model="clr-namespace:WpfTreeViewBinding.Model" 
        Title="MainWindow" 
        Height="350" Width="525">

    <Window.Resources>

        <HierarchicalDataTemplate DataType="{x:Type Model:DirectoryItem}"
                                  ItemsSource="{Binding Items}">
            <TextBlock Text="{Binding Path=Name}" ToolTip="{Binding Path=Path}" />
        </HierarchicalDataTemplate>

        <DataTemplate DataType="{x:Type Model:FileItem}">
            <TextBlock Text="{Binding Path=Name}" ToolTip="{Binding Path=Path}" />
        </DataTemplate>

    </Window.Resources>

    <Grid Margin="8">
        <TreeView ItemsSource="{Binding}" />
    </Grid>

</Window>

所有的魔力实际上都与数据模板有关。我想整件事的关键是将HierarchicalDataTemplate用于具有层次结构(即目录)的所有项目。

All of the magic really happens with the data templates. I guess the key to the whole thing is using the HierarchicalDataTemplate for any items with hierarchy (i.e. directories).

注意1:我尚未对此进行广泛测试。尚未对其进行性能分析。我很欢迎任何反馈,因为这是我很久以前试图解决并放弃的问题。谢谢!

NOTE 1: I haven't extensively tested this. It hasn't been profiled for performance. I would welcome any feedback though since this is a problem I tried to solve long ago and gave up on. Thanks!

注2:您需要将硬编码路径设置为对系统有意义的路径。

NOTE 2: You'll need to set the hard-coded path to something that makes sense on your system.

以下是屏幕快照,显示了不同级别的目录和文件...

Here is a screenshot showing directories and files at different levels...

这篇关于从WPF中的文件路径列表填充treeview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆