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.

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

Left by T. W. at 11/16/2010 5:37 AM

How do you select a particular folder (not a file) using this class library?

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

Left by Nir at 11/16/2010 10:09 PM

T.W. - the code here is based on the old OpenFileDialog/SaveFileDialog APIs, they don't have an option for selecting folders.

The newer IFileDialog has an option for selecting folders, the easiest way to use it is by using the Windows API Code Pack from http://code.msdn.microsoft.com/WindowsAPICodePack

or you can use any of the workarounds described in http://stackoverflow.com/questions/31059

# I think this is not necessary in latest WPF

Left by ADTC at 1/4/2012 11:41 AM

I'm using Visual C# 2010 and I think .NET Framework 4. While the WPF MessageBox continues to be displayed in Win2000 style (and can be fixed with the manifest edit), the open file dialog (Microsoft.Win32.OpenFileDialog) seems to take up the Vista/7 style correctly (in both debugging and normal run). Did they fix it in the latest iteration of WPF/.NET or am I missing something?

PS: You will need to update your manifest instructions. In Visual C# 2010, I select Add > New Item > Application Manifest File, and VC# populates the manifest file with some contents. Instead of replacing the existing contents (which may be important to the application), it's better to just add code from <dependency> to </dependency> right after </trustInfo> tag.

Your comment:



 (will not be displayed)


 
 
Please add 7 and 8 and type the answer here: