我开发了一个 WPF4 应用程序,在我的应用程序中,我需要让用户选择一个文件夹,应用程序将在其中存储某些内容(文件、生成的报告等)。
我的要求:
据我了解,直到 2010 年(.Net 4.0)都不会出现标准文件夹对话框,但也许 4.0 版本有一些变化?
或者我唯一能做的就是使用老式的WinForms
对话?如果这是满足我需要的唯一方法,我怎样才能使它看起来更接近 Vista/7 风格而不是 Win9x?
Windows 演示基础 4.5 手册 http://www.packtpub.com/windows-presentation-foundation-4-5-cookbook/book由 Pavel Yosifovich 在第 155 页上的“使用通用对话框”部分中说:
“文件夹选择(而不是文件)怎么样?WPF
OpenFileDialog 不支持这一点。一种解决方案是使用 Windows
表单的FolderBrowserDialog 类。另一个好的解决方案是使用
Windows API 代码包简要描述。”
I downloaded the API Code Pack from Windows® API Code Pack for Microsoft® .NET Framework http://archive.msdn.microsoft.com/WindowsAPICodePack Windows API Code Pack: Where is it? https://stackoverflow.com/q/24081665/109702, then added references to Microsoft.WindowsAPICodePack.dll and Microsoft.WindowsAPICodePack.Shell.dll to my WPF 4.5 project.
Example:
using Microsoft.WindowsAPICodePack.Dialogs;
var dlg = new CommonOpenFileDialog();
dlg.Title = "My Title";
dlg.IsFolderPicker = true;
dlg.InitialDirectory = currentDirectory;
dlg.AddToMostRecentlyUsedList = false;
dlg.AllowNonFileSystemItems = false;
dlg.DefaultDirectory = currentDirectory;
dlg.EnsureFileExists = true;
dlg.EnsurePathExists = true;
dlg.EnsureReadOnly = false;
dlg.EnsureValidNames = true;
dlg.Multiselect = false;
dlg.ShowPlacesList = true;
if (dlg.ShowDialog() == CommonFileDialogResult.Ok)
{
var folder = dlg.FileName;
// Do something with selected folder string
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)