Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

A WPF File ListView and ComboBox

0.00/5 (No votes)
28 Mar 2011 1  
The article describes a simple folder navigation control.

Introduction

There are many file listviews or file system navigation controls out there. Most are written to resemble the Windows Explorer. I needed a small and simple one for use in an application. It should be displayed in a narrow column on the left of the workspace and allow the user to open files in the current working directory, or close to it. The requirements are:

  • The control should use very little horizontal space, see the screenshot.
  • The control should be as lightweight as possible, and not clutter the user interface too much.
  • It should bind to a most recently used (MRU) file list, and allow the user to jump to locations of those files.
  • It should not be an Explorer clone, and the user should not be allowed to do Shell operations like copying, renaming files, etc. If you are looking for an Explorer clone, you can find an excellent one here.

Here is how the result looks, embedded in a larger UI, from which only a part is shown:

Here is a closer look at the combobox dropdown:

The three folders close to the bottom are extracted from the application's MRU list. The full paths of these items are not displayed, but are shown in a tooltip when hovering the mouse over.

Using the code

Download the above .zip-file and add a reference to the .dll to your project. The following snippet illustrates how to use the control:

<Window ...
        xmlns:FLV="clr-namespace:FileListView;assembly=FileListView"
        ... >
    ...
    <FLV:FolderView Filter="*.tex|*.txt|*.exe|*.*" 
            RecentLocations="{Binding RecentFiles}"
            OnFileOpen="folderView_OnFileOpen/>
    ...

Here, RecentFiles is your MRU list. The control extracts the directories from this list and removes the duplicates automatically. When the user selects a file by double click or pressing Return, the event OnFileOpen is fired. The following additional properties are available:

CurrentFolder The currently displayed folder.
FilterIndex The currently selected filter.
ShowFolders Turns on/off the display of folders.
ShowHidden Turns on/off the display of hidden files.
ShowIcons Turns on/off the display of file and folder icons.

Implementation details

Implementing a file listview is more or less a standard task, and there are only a few things about the code I should comment on. One is that I tried to keep everything lean and stuck with the onboard C# tools where possible. (The final library is only 31 KB small.) One exception is the use of Shell routines to extract associated file and folder icons. Another trick which speeds up display of larger folders a lot is to lazily query the icons associated to files, like this:

private ImageSource _DisplayIcon = null;
public ImageSource DisplayIcon
{
    get
    {
        if (_DisplayIcon == null)
        {
            try
            {
                if (type == FSItemType.Folder)
                    _DisplayIcon = IconExtractor.GetFolderIcon(FullPath).ToImageSource();
                else
                    _DisplayIcon = IconExtractor.GetFileIcon(FullPath).ToImageSource();

            }
            catch (Exception e)
            { 
                
            }
        }

        return _DisplayIcon;
    }

    set { _DisplayIcon = value; }
}

Similarly, the combobox dropdown list is only constructed when the user actually drops it down.

Links

In a previous version, I used (a slightly modified version of) this code. A very nice collection of Shell controls and routines has been released by Microsoft here.

History

  • 03/12/2011: Rewrote the icon extraction routines in order to also get the associated folder icons. Fixed an issue with the listview column width.
  • 03/28/2011: Implemented the listview as a separate control. Used the AutoGrayImage class from here.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here