Stories about Software


Directory Browser Dialog

One of the things that’s always surprised me about WPF is the lack of any kind of out-of-the-box file/directory browser dialog. It seems as though every time this comes up, I poke around the internet, hoping that it’s been added in some kind of update somewhere, but it never is. At least, it never is in the scope and skill of my google skills, anyway.

So, I wrote one for WPF. There’s nothing particularly hard about this, and I don’t view it as some kind of breakthrough. The reason that I’m making this post is that I noticed a trend that I find rather irritating. Whenever I’m searching for something like this, I seem to encounter two kinds of posts: posts that offer some zipped Visual Studio project I can download and plugin or else some vague, incomplete suggestion of what to do.

As for me personally, I’m just looking for functional code that I can slam right into my project. I don’t really want to download things and I don’t really want to spend the effort coding up something that I think should already exist. That is to say, I have no issue getting my hands dirty when I can learn something or customize, but I’m also not a big proponent of reinventing the wheel.

I’m posting here to break this trend that I noticed. This is a completely functional solution that exists not at all beyond what you see here. I’m hoping this appeals to people with tastes like mine. I like to see all the code but without downloading things.

So, without further ado:


Above is the XAML for the user control and below is the code behind

public partial class DirectoryDialog : UserControl

    /// This is to allow for binding of the directory path obtained by this control
    public static readonly DependencyProperty DirectoryPathProperty = DependencyProperty.Register("DirectoryPath", typeof(string), typeof(DirectoryDialog),
        new FrameworkPropertyMetadata((string)string.Empty, FrameworkPropertyMetadataOptions.AffectsRender));

    public string DirectoryPath { get { return (string)GetValue(DirectoryPathProperty) ?? string.Empty; } set { SetValue(DirectoryPathProperty, value); } }

    /// Initializes the directroy dialog user control and sets data context to self
    public DirectoryDialog()

    /// Handler for when user double clicks the file box
    /// I try to avoid code behind whenever possible.  Logic in declarative markup sucks for debugging and testing,
    /// and code behind is inherently hard to debug and test, but this is an exception.  I don't want this COM construct
    /// anywhere near something I'm planning to unit test. =) 
    private void HandleDoubleClick(object sender, MouseButtonEventArgs e)
        using (var myDialog = new System.Windows.Forms.FolderBrowserDialog())
            if (!string.IsNullOrEmpty(DirectoryPath))
                myDialog.SelectedPath = DirectoryPath;
            DirectoryPath = myDialog.SelectedPath;

An example user of this is here:


As you can see, this is about as simple as it gets. If I have use for it, I’ll probably later rename the control to something like ChooserDialog and allow it to have modes of “File” and “Directory” which pop up their respective kinds of dialogs. This could certainly be extended and made snazzier, and maybe I’ll do that with time. And, maybe when I do, I’ll post a link allowing people to download it, but I will also post the code so that you can inspect it without jumping through hoops, and you can see if it suits your needs.