Click here to Skip to main content
15,885,278 members
Articles / Desktop Programming / WPF

A WPF File ListView and ComboBox

Rate me:
Please Sign up or sign in to vote.
4.89/5 (8 votes)
28 Mar 2011CPOL2 min read 57.4K   2K   39   12
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:

XML
<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:

CurrentFolderThe currently displayed folder.
FilterIndexThe currently selected filter.
ShowFoldersTurns on/off the display of folders.
ShowHiddenTurns on/off the display of hidden files.
ShowIconsTurns 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:

C#
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)


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

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

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

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