I want the user to select a directory where a file that I will then generate will be saved. I know that in WPF I should use the OpenFileDialog
from Win32, but unfortunately the dialog requires file(s) to be selected - it stays open if I simply click OK without choosing one. I could "hack up" the functionality by letting the user pick a file and then strip the path to figure out which directory it belongs to but that's unintuitive at best. Has anyone seen this done before?
相关问题
- VNC control for WPF application
- WPF Binding from System.Windows.SystemParameters.P
- XAML: Applying styles to nested controls
- How can I add a horizontal line (“goal line”) for
- How to properly change a resource dictionary
None of these answers worked for me (generally there was a missing reference or something along those lines)
But this quite simply did:
Using FolderBrowserDialog in WPF application
Add a reference to
System.Windows.Forms
and use this code:No need to track down missing packages. Or add enormous classes
This gives me a modern folder selector that also allows you to create a new folder
I'm yet to see the impact when deployed to other machines
You can use the built-in FolderBrowserDialog class for this. Don't mind that it's in the
System.Windows.Forms
namespace.If you want the window to be modal over some WPF window, see the question How to use a FolderBrowserDialog from a WPF application.
EDIT: If you want something a bit more fancy than the plain, ugly Windows Forms FolderBrowserDialog, there are some alternatives that allow you to use the Vista dialog instead:
The Windows API Code Pack-Shell:
Note that this dialog is not available on operating systems older than Windows Vista, so be sure to check
CommonFileDialog.IsPlatformSupported
first.I'm using Ookii dialogs for a while and it work nice for WPF.
Here's the direct page:
http://www.ookii.org/Blog/new_download_ookiidialogs
I created a UserControl which is used like this:
The xaml source looks like this:
and the code-behind
Ookii folder dialog can be found at Nuget.
PM> Install-Package Ookii.Dialogs
And, example code is as below.
The best way to achieve what you want is to create your own wpf based control , or use a one that was made by other people
why ? because there will be a noticeable performance impact when using the winforms dialog in a wpf application (for some reason)
i recommend this project
https://opendialog.codeplex.com/
or Nuget :
it's very MVVM friendly and it isn't wraping the winforms dialog