Click here to Skip to main content
Click here to Skip to main content

A WPF File ListView and ComboBox

, 28 Mar 2011
Rate this:
Please Sign up or sign in to vote.
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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Thomas Willwacher

United States United States
No Biography provided

Comments and Discussions

 
QuestionRe-implemented/updated your solution PinpremiumDirkster9916-Apr-14 13:00 
GeneralMy vote of 5 Pinmemberamirmn728-Sep-12 18:48 
QuestionListView Theme PinmemberSBJ29-Aug-11 9:00 
AnswerRe: ListView Theme PinmemberThomas Willwacher30-Aug-11 10:07 
GeneralRe: ListView Theme PinmemberSBJ30-Aug-11 18:53 
GeneralI like this ......but almost think this would be better as 3 controls PinmvpSacha Barber14-Mar-11 3:23 
GeneralRe: I like this ......but almost think this would be better as 3 controls PinmemberThomas Willwacher27-Mar-11 19:57 
GeneralRe: I like this ......but almost think this would be better as 3 controls PinmvpSacha Barber27-Mar-11 20:56 
GeneralRe: I like this ......but almost think this would be better as 3 controls PinmemberThomas Willwacher27-Mar-11 22:12 
GeneralRe: I like this ......but almost think this would be better as 3 controls PinmvpSacha Barber27-Mar-11 22:25 
GeneralRe: I like this ......but almost think this would be better as 3 controls PinmemberThomas Willwacher27-Mar-11 22:45 
GeneralRe: I like this ......but almost think this would be better as 3 controls PinmvpSacha Barber27-Mar-11 22:52 

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 | Mobile
Web01 | 2.8.140827.1 | Last Updated 28 Mar 2011
Article Copyright 2011 by Thomas Willwacher
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid