Vista style open and save dialogs with WPF (without using the Vista bridge sample)

This is the fourth (and last) part in a series about how to get the latest look and feel for your WPF application, the previous parts are:

In part 3 of this series we’ve set a manifest file and almost solved our problem – we finally got rid of the Windows 2000 style dialogs and moved to XP, but we still haven’t got Vista style file open and save dialogs.

The problem is again backward compatibility, the Windows file dialogs are customizable and when an application customizes a dialog Windows will helpfully use the version of the dialog supported by the application in order not to break the customizations.

You probably didn’t customize the file dialogs but WPF (as well as WinForms) always tell Windows they customized the dialog.

All you have to do now is to bypass the FileOpenDialog and FileSaveDialog completely and use the Windows APIs directly – this is easier than it sounds – you basically have two options, you can find the Vista bridge sample on MSDN, this sample contains all the code needed to use and customize the Vista file dialogs (there is a lot of code there), on the other hand, you can use PInvoke to call the old simple APIs and as long as you don’t try to customize the dialog you will always get the latest version.

Download the code

The zip files contain classes that are mostly a drop in replacements for the WPF classes, the code is straight forward interop code that calls the GetOpenFileName or GetSaveFileName Windows functions, the only tricky part is that those functions sometimes return string with embedded nulls, I’ll explain how to deal with this in a future post.

posted @ Tuesday, July 15, 2008 4:26 PM

Comments on this entry:

# re: Vista style open and save dialogs with WPF (without using the Vista bridge sample)

Left by Logan at 4/14/2010 1:12 AM

Thanks for this--it's a much simpler solution than using the API Codepack. However, I cannot seem to figure out how to set the default starting file name in save dialogs. This was done by setting the FileName property on the previous SaveFileDialog class.

# re: Vista style open and save dialogs with WPF (without using the Vista bridge sample)

Left by Nir at 4/14/2010 2:30 PM

Hi Logan.

I fixed it, you can download the new version from the same location.

# re: Vista style open and save dialogs with WPF (without using the Vista bridge sample)

Left by Logan at 4/16/2010 1:33 AM

Hi Nir. I'm still having some difficulties with the file name. Can you explain how I should use it? Here is what I am currently doing (I'm not entirely sure the default extension is working either, if it's supposed to affect the starting filename, but that could just be because there is no default filename):
MultiTimer.Dialogs.SaveFileDialog dlg = new MultiTimer.Dialogs.SaveFileDialog();
dlg.FileName = this.Title; // Default file name
dlg.DefaultExt = ".txt"; // Default file extension
dlg.Filter = "Text documents (.txt)|*.txt|All files (*.*)|*.*"; // Filter files by extension

Thanks!

# re: Vista style open and save dialogs with WPF (without using the Vista bridge sample)

Left by Logan at 4/16/2010 6:38 PM

Hi again. Don't worry about my previous comment. I found a handy library called Ookii.Dialogs that has all sorts of goodies including Vista style open and save dialogs that work great. Thanks again for your solution.

Your comment:



 (will not be displayed)


 
 
 
Please add 8 and 4 and type the answer here: