Folder browser to list all system drive in WPF

旧街凉风 提交于 2019-12-22 09:59:31

问题


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.


回答1:


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!



来源:https://stackoverflow.com/questions/22375801/folder-browser-to-list-all-system-drive-in-wpf

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!