Click here to Skip to main content
11,716,938 members (80,866 online)
Click here to Skip to main content

WPF Folder Browser

, 6 Nov 2010 Ms-PL 82.5K 5.9K 50
Rate this:
Please Sign up or sign in to vote.
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)

Share

About the Author

mohammed shareef
Software Developer
United Kingdom United Kingdom
No Biography provided

You may also be interested in...

Comments and Discussions

 
GeneralThis is good Pin
MayFly112424-Nov-14 17:42
memberMayFly112424-Nov-14 17:42 
GeneralMy vote of 3 Pin
thund3rstruck9-Oct-14 9:39
memberthund3rstruck9-Oct-14 9:39 
QuestionMemory Usage Jumps to almost 2GB Pin
thund3rstruck9-Oct-14 9:39
memberthund3rstruck9-Oct-14 9:39 
QuestionLife saving example Pin
Hatzy14-Jun-12 21:59
memberHatzy14-Jun-12 21:59 
GeneralMy vote of 5 Pin
jonnyboy8220-Mar-12 23:50
memberjonnyboy8220-Mar-12 23:50 
GeneralMy vote of 5 Pin
Erik Rude12-Mar-12 5:41
memberErik Rude12-Mar-12 5:41 
Thanks for sharing
QuestionUnable to open your codes Pin
jason27265-Jan-12 14:55
memberjason27265-Jan-12 14:55 
AnswerRe: Unable to open your codes Pin
Derek Cooper23-Jan-12 3:43
memberDerek Cooper23-Jan-12 3:43 
Questionthank you Pin
anthride6-Nov-11 5:06
memberanthride6-Nov-11 5:06 
QuestionNew Functionality Pin
sean ireson15-Sep-11 9:28
membersean ireson15-Sep-11 9:28 
AnswerRe: New Functionality Pin
Erik Rude12-Mar-12 5:40
memberErik Rude12-Mar-12 5:40 
GeneralDoes this code sample availble in 3.5 ? Pin
danies813-Sep-11 4:22
memberdanies813-Sep-11 4:22 
Questionthanks Pin
zhujinlong1984091316-Aug-11 21:18
groupzhujinlong1984091316-Aug-11 21:18 
QuestionThanks this saved me some time. Found a bug. Pin
Arsine59-Jul-11 12:34
memberArsine59-Jul-11 12:34 
GeneralVery-2 good Article Pin
Jaikrishan3-Jun-11 0:25
memberJaikrishan3-Jun-11 0:25 
GeneralMy vote of 5 Pin
Slacker00727-Dec-10 7:16
memberSlacker00727-Dec-10 7:16 
GeneralGood Pin
shakil03040037-Nov-10 3:23
membershakil03040037-Nov-10 3:23 
QuestionDo you know about CommonOpenFileDialog ? Pin
Aybe19-Oct-10 6:31
memberAybe19-Oct-10 6:31 
AnswerRe: Do you know about CommonOpenFileDialog ? Pin
mohammed shareef22-Oct-10 5:11
membermohammed shareef22-Oct-10 5:11 
GeneralPrism Binaries Pin
sam.hill15-Oct-10 7:38
membersam.hill15-Oct-10 7:38 
GeneralRe: Prism Binaries Pin
mohammed shareef15-Oct-10 13:00
membermohammed shareef15-Oct-10 13:00 

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.

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