文件夹浏览器列出所有WPF系统驱动器 [英] Folder browser to list all system drive in WPF

查看:226
本文介绍了文件夹浏览器列出所有WPF系统驱动器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创造了WPF的文件夹浏览器控件,并工作正常,但只有一个驱动器,我硬编码。



我遵循的文件这样做是:



http://msdn.microsoft.com/en-us/library/bb546972%28v=vs.90%29.aspx



< 。p>我想让它列出系统中的所有驱动器中的树视图,而不是只有一个

 < Window.Resources> ; 
< ObjectDataProvider的X:键=RootFolderDataProvider>
< ObjectDataProvider.ObjectInstance>
< folderExplorer:FolderExplorer FULLPATH =E:\/>
< /ObjectDataProvider.ObjectInstance>
< / ObjectDataProvider的>

< HierarchicalDataTemplate
数据类型={X:类型folderExplorer:FolderExplorer}
的ItemsSource ={绑定路径=子文件夹}>
< TextBlock的文本={绑定路径=名称}/>
< / HierarchicalDataTemplate>
< /Window.Resources>

将; TreeView的Grid.Column =0
名称=RootTreeView
背景=艾莉斯蓝
前景=黑Grid.RowSpan = 3保证金=0,0,0,169>
<树型视图标题=浏览>
< TreeViewItem.ItemsSource>
<绑定源={StaticResource的RootFolderDataProvider}>
< Binding.Path>及子文件夹下; /Binding.Path>
< /绑定>
< /TreeViewItem.ItemsSource>
< /树型视图>
< / TreeView的>

如果我填充代码树视图的背后,我的所有其他代码是打破..



如何使这个列表中的所有驱动器将是非常有益的任何建议。


解决方案

首先,我们将需要一个新的类,称之为DriveExplorer软件。 。我保持了文件夹的名字从链接的样本,从你的XAML,您可能需要使用FolderExplorer来取代它。



首先,代码:

 公共类DriveExplorer软件
{
私人的ObservableCollection<&夹GT; _folders;
公众的ObservableCollection<&夹GT;文件夹
{
得到
{
_folders =新的ObservableCollection<&夹GT;();

DriveInfo [] =驱动DriveInfo.GetDrives();
的foreach(DriveInfo驱动器的驱动器)
{
//我们只想与文件夹驱动器,固定是硬盘驱动器
如果(drive.DriveType == DriveType.Fixed)
{
文件夹newFolder =新的文件夹();
newFolder.FullPath = drive.Name;
_folders.Add(newFolder);
}
}
}
}
}

现在为它做什么。就像文件夹我们宣布的名单的ObservableCollection<文件夹方式> 来存储我们的驱动器中对于所有意图和目的,一个驱动器只是我们得到一个文件夹以不同的方式。然后,我们开始使用 DriveInfo.GetDrives系统上的驱动器列表()



然后,我们遍历使用foreach整个集合(这样做的同样的事情在示例代码循环),使用驱动作为我们的迭代变量(< A HREF =http://msdn.microsoft.com/en-us/library/ttw7t8t6.aspx相对=nofollow> MSDN )。我以为,我们只是想硬盘,所以我们检查固定了的DriveType。如果我们不关心的类型,这种检查可以被删除。有关该功能的完整参考,请参阅的 MSDN 。最后,我们做一个新的文件夹设置为盘符路径,就像你在XAML做(和样品确实在其构造)。



现在的XAML,我们将需要一个非常类似的数据模板到一个你已经有(这是除了现有的):

 < HierarchicalDataTemplate 
数据类型={X:类型folderExplorer:DriveExplorer软件}
的ItemsSource ={绑定路径=文件夹}>
< TextBlock的文本={绑定路径=名称}/>
< / HierarchicalDataTemplate>



然后,我们只是需要将数据源更改为DriveExplorer软件:

 < ObjectDataProvider的X:键=RootFolderDataProvider> 
< ObjectDataProvider.ObjectInstance>
< folderExplorer:DriveExplorer软件/>
< /ObjectDataProvider.ObjectInstance>
< / ObjectDataProvider的>

这应该给你你想要的输出。让我知道如果我需要做任何更正或能澄清什么!


I have created a FOLDER BROWSER control in WPF, and is working fine, but for only one drive that I hard code.

The document I followed to do so is :

http://msdn.microsoft.com/en-us/library/bb546972%28v=vs.90%29.aspx

I want to make it list all drives in the system in the treeview instead of only one.

<Window.Resources>
        <ObjectDataProvider x:Key="RootFolderDataProvider">
            <ObjectDataProvider.ObjectInstance>
                <folderExplorer:FolderExplorer FullPath="e:\" />
            </ObjectDataProvider.ObjectInstance>
        </ObjectDataProvider>

        <HierarchicalDataTemplate
            DataType    = "{x:Type folderExplorer:FolderExplorer}"
            ItemsSource = "{Binding Path=SubFolders}">
            <TextBlock Text="{Binding Path=Name}" />
        </HierarchicalDataTemplate>
    </Window.Resources>

<TreeView Grid.Column="0"
                  Name="RootTreeView"
                  Background="AliceBlue"
                  Foreground="Black" Grid.RowSpan="3" Margin="0,0,0,169">
            <TreeViewItem Header="Browse">
                <TreeViewItem.ItemsSource>
                    <Binding Source="{StaticResource RootFolderDataProvider}">
                        <Binding.Path>SubFolders</Binding.Path>
                    </Binding>
                </TreeViewItem.ItemsSource>
            </TreeViewItem>
        </TreeView>

If I populate the treeview in the code behind, all my other code is breaking..

Any suggestion on how to make this list all drive will be very helpful.

解决方案

First, we are going to need a new class, call it "DriveExplorer". I am keeping the "Folder" name from the linked sample, from your XAML you may need to replace it with "FolderExplorer".

First, the code:

public class DriveExplorer
{
    private ObservableCollection<Folder> _folders;
    public ObservableCollection<Folder> Folders
    {
        get
        {
            _folders = new ObservableCollection<Folder>();

            DriveInfo[] drives = DriveInfo.GetDrives();
            foreach (DriveInfo drive in drives)
            {
                //We only want drives with folders, "Fixed" is hard drives
                if (drive.DriveType == DriveType.Fixed)
                {
                    Folder newFolder = new Folder();
                    newFolder.FullPath = drive.Name;
                    _folders.Add(newFolder);
                }
            }
        }
    }
}

Now for what it does. Just like "Folder" we declare a list of ObservableCollection<Folder> to store our "drives" in. For all intents and purposes, a drive is just a folder we get in a different way. Then, we get the list of drives on the system using DriveInfo.GetDrives().

We then iterate over the whole collection using a foreach (this does the same thing as the for loop in the sample code) using "drive" as our iteration variable (MSDN). I assumed that we just want hard drives, so we check the DriveType for "Fixed". If we don't care about the type, this check could be removed. For a full reference on this function, see MSDN. Finally, we make a new "Folder" with the path set to the drive letter, just like you do in your XAML (and the sample does in its constructor).

Now for the XAML, we will need a very similar Data Template to the one you already have(this is in addition to the existing one):

<HierarchicalDataTemplate
     DataType    = "{x:Type folderExplorer:DriveExplorer}"
     ItemsSource = "{Binding Path=Folders}">
     <TextBlock Text="{Binding Path=Name}" />
 </HierarchicalDataTemplate>

Then we just need to change the data source to a "DriveExplorer":

<ObjectDataProvider x:Key="RootFolderDataProvider">
    <ObjectDataProvider.ObjectInstance>
         <folderExplorer:DriveExplorer />
    </ObjectDataProvider.ObjectInstance>
</ObjectDataProvider>

This should give you the output you want. Let me know if I need to make any corrections or can clarify anything!

这篇关于文件夹浏览器列出所有WPF系统驱动器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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