Click here to Skip to main content
Licence Ms-PL
First Posted 15 Oct 2010
Views 28,578
Downloads 1,853
Bookmarked 38 times

WPF Folder Browser

By | 6 Nov 2010 | Article
A WPF folder browser

Introduction

This is a folder browser for WPF similar to the one found in Windows forms. WPF currently does not provide a Folder browser dialog and the only thing near enough is the Microsoft.Win32.OpenFileDialog class in WPF's PresentationFramework.dll assembly which lets users specify multiple files to open.

Using the Code

The code contains only one XAML file which houses different parts of the dialog. The dialog is loosely based on the Windows file/ folder dialog model as it has a commonly selected folder area and a Windows explorer style treeview. The selected folder is displayed in the textbox at the bottom of the dialog. The code uses MVVM and has a dependency on the latest prism binaries but this is for the DelegateCommand only. The main ViewModel which the dialog binds to is the BrowserViewModel, this initializes the treeview to point to all the drives on the machine. It has a SelectedFolder property & a FolderSelected DelegateCommand which is invoked when the user selects a common folder on the left.

public DelegateCommand<object> FolderSelectedCommand
     {
         get
         {
             return new DelegateCommand<object>(it => SelectedFolder = 
		Environment.GetFolderPath((Environment.SpecialFolder)it));
         }
     }

The folders themselves are expressed in terms of a FolderViewModel which takes an ObservableCollection of FolderViewModels to represent sub folders. The treeview in the folder dialog is bound to this FolderViewModel. The FolderViewModel implements IsExpanded & IsSelected properties to add functionality to the TreeViewItem state, i.e., to dynamically load sub folders on selection/expansion of a node. Selecting a node also expands it similar to the Windows Explorer.

private void LoadFolders()
     {
         try
         {
             if (Folders.Count > 0)
                 return;

             string[] dirs = null;

             string fullPath = Path.Combine(FolderPath, FolderName);

             if (FolderName.Contains(':'))//This is a drive
                 fullPath = string.Concat(FolderName, "\\");
             else
                 fullPath = FolderPath;

             dirs = Directory.GetDirectories(fullPath);

             Folders.Clear();
             
             foreach (string dir in dirs)
                 Folders.Add(new FolderViewModel 
                 { Root = this.Root, FolderName = Path.GetFileName(dir), 
                 FolderPath = Path.GetFullPath(dir), 
                 FolderIcon = "Images\\FolderClosed.png" });

             if (FolderName.Contains(":"))
                 FolderIcon = "Images\\HardDisk.ico";

             Root.SelectedFolder = FolderPath;
         }
         catch (UnauthorizedAccessException ae)
         {
             Console.WriteLine(ae.Message);
         }
         catch (IOException ie)
         {
             Console.WriteLine(ie.Message);
         }
     }

Points of Interest

One of the things I came across while creating this control was that the WPF binding for the SpecialFolder enum in the Environment class did not work. Initially I thought that this was due to the enum being nested, but actually it is due to the fact that nested types are not supported in XAML. A workaround for this was to append a '+' instead of a '.' for the nested enum (e.g.: Environment+SpecialFolder.Desktop).

History

  • 14th October, 2010: Initial version
  • 15th October, 2010: Added Prism binaries to download zip

License

This article, along with any associated source code and files, is licensed under The Microsoft Public License (Ms-PL)

About the Author

mohammed shareef

Software Developer

United Kingdom United Kingdom

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralMy vote of 5 Pinmemberjonnyboy8223:50 20 Mar '12  
GeneralMy vote of 5 PinmemberErik Rude5:41 12 Mar '12  
QuestionUnable to open your codes Pinmemberjason272614:55 5 Jan '12  
AnswerRe: Unable to open your codes PinmemberDerek Cooper3:43 23 Jan '12  
Questionthank you Pinmemberanthride5:06 6 Nov '11  
QuestionNew Functionality Pinmembersean ireson9:28 15 Sep '11  
AnswerRe: New Functionality PinmemberErik Rude5:40 12 Mar '12  
GeneralDoes this code sample availble in 3.5 ? Pinmemberdanies84:22 13 Sep '11  
Questionthanks Pingroupzhujinlong1984091321:18 16 Aug '11  
QuestionThanks this saved me some time. Found a bug. PinmemberArsine512:34 9 Jul '11  
GeneralVery-2 good Article PinmemberJaikrishan0:25 3 Jun '11  
GeneralMy vote of 5 PinmemberSlacker0077:16 27 Dec '10  
GeneralGood Pinmembershakil03040033:23 7 Nov '10  
QuestionDo you know about CommonOpenFileDialog ? PinmemberAybe6:31 19 Oct '10  
AnswerRe: Do you know about CommonOpenFileDialog ? Pinmembermohammed shareef5:11 22 Oct '10  
GeneralPrism Binaries Pinmembersam.hill7:38 15 Oct '10  
GeneralRe: Prism Binaries Pinmembermohammed shareef13:00 15 Oct '10  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web04 | 2.5.120517.1 | Last Updated 6 Nov 2010
Article Copyright 2010 by mohammed shareef
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid