选择文件夹对话框 WPF
我开发了一个 WPF4 应用程序,在我的应用程序中,我需要让用户选择一个文件夹,应用程序将在其中存储某些内容(文件、生成的报告等)。
我的要求:
能够查看标准文件夹树
能够选择文件夹
WPF 外观和样式感觉上,这个对话框一定看起来像为 Windows Vista/7 而不是 Windows 2000 甚至 Win9x 设计的现代应用程序的一部分。
据我了解,直到 2010 年(.Net 4.0)都不会出现标准文件夹对话框,但也许 4.0 版本有一些变化?
或者我唯一能做的就是使用老式的 WinForms 对话框?如果这是满足我需要的唯一方法,我怎样才能使它看起来更接近 Vista/7 风格而不是 Win9x?
I develop a WPF4 application and in my app I need to let the user select a folder where the application will store something (files, generated reports etc.).
My requirements:
Ability to see the standard folder tree
Ability to select a folder
WPF look & feel, this dialog must look like part of a modern application designed for Windows Vista/7 and not Windows 2000 or even Win9x.
As I understand, until 2010 (.Net 4.0) there won't be a standard folder dialog, but maybe there are some changes in version 4.0?
Or the only thing I can do, is to use an old-school WinForms
dialog? If it's the only way to do what I need, how can I make it looking closer to Vista/7 style and not Win9x?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(12)
Windows Presentation Foundation 4.5 Cookbook,作者:Pavel Yosifovich,页面155 在“使用通用对话框”部分中说:
我从
Windows® API 代码包下载了 API 代码包Microsoft® .NET FrameworkWindows API 代码包:它在哪里?< /a>,然后将对 Microsoft.WindowsAPICodePack.dll 和 Microsoft.WindowsAPICodePack.Shell.dll 的引用添加到我的 WPF 4.5 项目中:
Windows Presentation Foundation 4.5 Cookbook by Pavel Yosifovich on page 155 in the section on "Using the common dialog boxes" says:
I downloaded the API Code Pack from
Windows® API Code Pack for Microsoft® .NET FrameworkWindows API Code Pack: Where is it?, then added references to Microsoft.WindowsAPICodePack.dll and Microsoft.WindowsAPICodePack.Shell.dll to my WPF 4.5 project.Example:
我很久以前在我的博客上写过,WPF 对常见文件对话框的支持非常糟糕(或者至少是在 3.5 中,我没有在版本 4 中检查),但是很容易解决它。
您需要将正确的清单添加到您的应用程序中,这将为您提供现代风格的消息框和文件夹浏览器(
WinForms
、FolderBrowserDialog
),但不是 WPF 文件打开/保存对话框,这在这 3 篇文章中进行了描述(如果您不关心解释而只想解决方案,请直接转到第 3 篇文章):为什么我使用 WPF 获得旧式文件对话框和消息框
设置清单可以解决我的 WPF 消息框样式问题吗?
使用 WPF 的 XP 和 Vista 样式文件对话框和消息框所需的应用程序清单
幸运的是,打开/保存对话框是围绕 Win32 API 的非常薄的包装器,可以使用正确的标志轻松调用以获取Vista/7 样式(设置清单后)
I wrote about it on my blog a long time ago, WPF's support for common file dialogs is really bad (or at least is was in 3.5 I didn't check in version 4), but it's easy to work around it.
You need to add the correct manifest to your application, that will give you a modern style message boxes and folder browser (
WinForms
,FolderBrowserDialog
) but not WPF file open/save dialogs, this is described in those 3 posts (if you don't care about the explanation and only want the solution go directly to the 3rd):Why am I Getting Old Style File Dialogs and Message Boxes with WPF
Will Setting a Manifest Solve My WPF Message Box Style Problems?
The Application Manifest Needed for XP and Vista Style File Dialogs and Message Boxes with WPF
Fortunately, the open/save dialogs are very thin wrappers around the Win32 API that is easy to call with the right flags to get the Vista/7 style (after setting the manifest)
将 Windows API Code Pack-Shell 添加到您的项目中
Add The Windows API Code Pack-Shell to your project
如果您不想使用 Windows 窗体,也不想编辑清单文件,我想出了一个非常简单的方法,使用 WPF 的 SaveAs 对话框来实际选择目录。
不需要使用指令,您只需复制粘贴下面的代码即可!
它应该仍然非常用户友好,大多数人永远不会注意到。
这个想法来自于这样一个事实:我们可以很容易地更改该对话框的标题、隐藏文件并解决生成的文件名。
这肯定是一个很大的黑客,但也许它会很好地满足您的使用...
在这个示例中,我有一个文本框对象来包含结果路径,但您可以删除相关行并使用返回值,如果你希望...
这个黑客的唯一问题是:
大多数人不会注意到这些,尽管如果微软愿意的话,我肯定更喜欢使用官方的 WPF 方式,但在他们这样做之前,这就是我的临时解决办法。
If you don't want to use Windows Forms nor edit manifest files, I came up with a very simple hack using WPF's SaveAs dialog for actually selecting a directory.
No using directive needed, you may simply copy-paste the code below !
It should still be very user-friendly and most people will never notice.
The idea comes from the fact that we can change the title of that dialog, hide files, and work around the resulting filename quite easily.
It is a big hack for sure, but maybe it will do the job just fine for your usage...
In this example I have a textbox object to contain the resulting path, but you may remove the related lines and use a return value if you wish...
The only issues with this hack are :
Most people won't notice these, although I would definitely prefer using an official WPF way if microsoft would get their heads out of their asses, but until they do, that's my temporary fix.
建议使用
System.Windows.Forms
中的FolderBrowserDialog
类来显示允许用户选择文件夹的对话框。直到最近,该对话框的外观和行为与其他文件系统对话框不一致,这也是人们不愿意使用它的原因之一。
好消息是
FolderBrowserDialog
在 NET Core 3.0 中进行了“现代化”,因此对于那些编写针对该版本或更高版本的 Windows 窗体或 WPF 应用程序的人来说,现在是一个可行的选择。至参考< code>System.Windows.Forms 在 NET Core WPF 应用中,需要编辑项目文件并添加以下行:
这可以直接放置在现有
之后;
元素。那么这只是使用对话框的情况:
FolderBrowserDialog
有一个RootFolder
属性,据说“设置浏览开始的根文件夹”但是无论我将其设置为什么,都没有任何区别;SelectedPath
似乎是用于此目的的更好的属性,但是尾部反斜杠是必需的。此外,ShowNewFolderButton 属性似乎也被忽略,无论如何,该按钮始终显示。
The
FolderBrowserDialog
class fromSystem.Windows.Forms
is the recommended way to display a dialog that allows a user to select a folder.Until recently, the appearance and behaviour of this dialog was not in keeping with the other file system dialogs, which is one of the reasons why people were reluctant to use it.
The good news is that
FolderBrowserDialog
was "modernized" in NET Core 3.0, so is now a viable option for those writing either Windows Forms or WPF apps targeting that version or later.To reference
System.Windows.Forms
in a NET Core WPF app, it is necessary to edit the project file and add the following line:This can be placed directly after the existing
<UseWPF>
element.Then it's just a case of using the dialog:
FolderBrowserDialog
has aRootFolder
property that supposedly "sets the root folder where the browsing starts from" but whatever I set this to it didn't make any difference;SelectedPath
seemed to be the better property to use for this purpose, however the trailing backslash is required.Also, the
ShowNewFolderButton
property seems to be ignored as well, the button is always shown regardless.Microsoft.Win32.OpenFileDialog 是 Windows 上的任何应用程序都使用的标准对话框。当您在 .NET 4.0 中使用 WPF 时,您的用户不会对其外观感到惊讶
。该对话框在 Vista 中已更改。 .NET 3.0 和 3.5 中的 WPF 仍然使用旧版对话框,但在 .NET 4.0 中已修复。我只能猜测您启动了该线程,因为您正在看到那个旧对话框。这可能意味着您实际上正在运行一个针对 3.5 的程序。是的,Winforms 包装器确实获得了升级并显示了 Vista 版本。 System.Windows.Forms.OpenFileDialog 类,您需要添加对 System.Windows.Forms 的引用。
Microsoft.Win32.OpenFileDialog is the standard dialog that any application on Windows uses. Your user won't be surprised by its appearance when you use WPF in .NET 4.0
The dialog was altered in Vista. WPF in .NET 3.0 and 3.5 still used the legacy dialog but that was fixed in .NET 4.0. I can only guess that you started this thread because you are seeing that old dialog. Which probably means you're actually running a program that is targeting 3.5. Yes, the Winforms wrapper did get the upgrade and shows the Vista version. System.Windows.Forms.OpenFileDialog class, you'll need to add a reference to System.Windows.Forms.
MVVM + WinForms FolderBrowserDialog 作为行为
使用
博客文章: http://kostylizm.blogspot.ru/2014/03/wpf-mvvm-and-winforms-folder-dialog-how.html
MVVM + WinForms FolderBrowserDialog as behavior
Usage
Blogpost: http://kostylizm.blogspot.ru/2014/03/wpf-mvvm-and-winforms-folder-dialog-how.html
根据 Oyun 的回答,最好对FolderName 使用依赖属性。这允许(例如)绑定到子属性,这在原始版本中不起作用。另外,在我调整的版本中,对话框显示选择初始文件夹。
XAML 中的用法:
代码:
Based on Oyun's answer, it's better to use a dependency property for the FolderName. This allows (for example) binding to sub-properties, which doesn't work in the original. Also, in my adjusted version, the dialog shows selects the initial folder.
Usage in XAML:
Code:
Ookii Dialogs for WPF 有一个
VistaFolderBrowserDialog
类,它提供WPF 文件夹浏览器对话框的完整实现。还有一个适用于 Windows 窗体 的版本。
The Ookii Dialogs for WPF has a
VistaFolderBrowserDialog
class that provides a complete implementation of a folder browser dialog for WPF.There's also a version that works with Windows Forms.
只有这样的对话框是FileDialog。它是 WinForms 的一部分,但实际上它只是 WinAPI 标准操作系统文件对话框的包装。我不认为它丑陋,它实际上是操作系统的一部分,所以它看起来像它运行的操作系统。
否则,没有什么可以帮助你的。您要么需要寻找第三方实施,要么免费(我认为没有任何好处),要么付费。
Only such dialog is FileDialog. Its part of WinForms, but its actually only wrapper around WinAPI standard OS file dialog. And I don't think it is ugly, its actually part of OS, so it looks like OS it is run on.
Other way, there is nothing to help you with. You either need to look for 3rd party implementation, either free (and I don't think there are any good) or paid.
C. Augusto Proiete 对原始问题的评论建议使用 Ookii 对话框 (https://github .com/ookii-dialogs/ookii-dialogs-wpf)。这就是我最终在我的情况下使用的。这是我在我的应用程序中使用它的方式。
A comment on the original question from C. Augusto Proiete suggested Ookii dialogs (https://github.com/ookii-dialogs/ookii-dialogs-wpf). That's what I ended up using in my situation. Here's how I used it in my app.
只是说一件事,
WindowsAPICodePack
无法在 Windows 7 6.1.7600 上打开CommonOpenFileDialog
。Just to say one thing,
WindowsAPICodePack
can not openCommonOpenFileDialog
on Windows 7 6.1.7600.