Reputation: 204756
I would like to use a Folder Browse Dialog for WPF, but there does not seem to be such an essential class for WPF
.
Some recommend to use System.Windows.Forms.FolderBrowserDialog
but this is a really awful dialog.
I tried Ookii.Dialogs.Wpf.VistaFolderBrowserDialog:
Ookii.Dialogs.Wpf.VistaFolderBrowserDialog dlg = new VistaFolderBrowserDialog();
dlg.SelectedPath = path;
dlg.ShowDialog();
but setting the SelectedPath
does not set the start folder when the Dialog opens which is essential for my program.
How can I get VistaFolderBrowserDialog
to open in the correct folder?
Upvotes: 3
Views: 2393
Reputation: 76
WpfFolderBrowser.WpfFolderBrowserDialog - this is the one that worked for me best!
I did one fix though: The function ErrorHelper.HResultFromWin32(int) generate System.OverflowException when pressing "Select Folder" or "Cancel" (run in .NET 4.5.1).
To fix, I put the problematic code in unchecked block:
unchecked
{
win32ErrorCode =
(int)((win32ErrorCode & 0x0000FFFF) | (FACILITY_WIN32 << 16) | 0x80000000);
}
Upvotes: 0
Reputation: 204756
I ended up using the Codeplex project WPF Native Folder Browser:
WPFFolderBrowser.WPFFolderBrowserDialog dlg = new WPFFolderBrowserDialog();
dlg.InitialDirectory = path;
bool? pathWasSelected = dlg.ShowDialog();
string selectedPath = null;
if(pathWasSelected == true)
selectedPath = dlg.FileName;
Upvotes: 2
Reputation: 6060
I cannot reproduce this. But maybe there is just some confusion here. If I do this:
var dialog = new VistaFolderBrowserDialog();
dialog.SelectedPath = @"C:\Data";
dialog.ShowDialog();
It will launch in "C:" having selected the Folder "data". When pressing OK, the result is "C:\Data". However, including the backslash at the end:
var dialog = new VistaFolderBrowserDialog();
dialog.SelectedPath = @"C:\Data\";
dialog.ShowDialog();
will launch the dialog within this exact folder and selecting nothing by default. When pressing "OK", the result is again "C:\Data".
Upvotes: 7