我希望用户选择一个目录,我将生成的文件将保存在其中。我知道在WPF中,我应该使用Win32中的OpenFileDialog,但不幸的是,对话框需要选择文件-如果我只是单击确定而不选择一个,它就会保持打开。我可以通过让用户选择一个文件,然后剥离路径以找出它属于哪个目录来“hack”该功能,但这充其量是不直观的。有人见过这种情况吗?
当前回答
对于目录对话框获取目录路径,首先添加引用System.Windows。表单,然后解析,然后把这个代码放在按钮点击。
var dialog = new FolderBrowserDialog();
dialog.ShowDialog();
folderpathTB.Text = dialog.SelectedPath;
(folderpathTB是文本框的名字,我想把文件夹路径,或者你可以把它分配给一个字符串变量也就是)
string folder = dialog.SelectedPath;
如果你想获得文件名/路径,只需在按钮单击上这样做
FileDialog fileDialog = new OpenFileDialog();
fileDialog.ShowDialog();
folderpathTB.Text = fileDialog.FileName;
(folderpathTB是文本框的名字,我想把文件路径,或者你可以把它分配给一个字符串变量太)
注意:对于文件夹对话框,必须将System.Windows.Forms.dll添加到项目中,否则它将无法工作。
其他回答
为此,您可以使用内置的FolderBrowserDialog类。不要介意它在System.Windows.Forms命名空间中。
using (var dialog = new System.Windows.Forms.FolderBrowserDialog())
{
System.Windows.Forms.DialogResult result = dialog.ShowDialog();
}
如果你想让窗口在某些WPF窗口上是模态的,请参阅如何从WPF应用程序中使用FolderBrowserDialog问题。
编辑:如果你想要一些比普通的、丑陋的Windows窗体文件夹浏览器对话框更花哨的东西,有一些选择允许你使用Vista对话框:
第三方库,如Ookii对话框(。NET 4.5 +) Windows API代码包- shell: 使用Microsoft.WindowsAPICodePack.Dialogs; ... var dialog = new CommonOpenFileDialog(); 对话框。IsFolderPicker = true; commonfiledialgresult result = dialog.ShowDialog(); 注意,这个对话框在Windows Vista之前的操作系统上是不可用的,所以一定要检查CommonFileDialog。IsPlatformSupported第一。
我建议,在黄金套餐中加入:
Install-Package OpenDialog
那么使用它的方法是:
Gat.Controls.OpenDialogView openDialog = new Gat.Controls.OpenDialogView();
Gat.Controls.OpenDialogViewModel vm = (Gat.Controls.OpenDialogViewModel)openDialog.DataContext;
vm.IsDirectoryChooser = true;
vm.Show();
WPFLabel.Text = vm.SelectedFilePath.ToString();
以下是文档: http://opendialog.codeplex.com/documentation
适用于文件,文件过滤器,文件夹等
Ookii VistaFolderBrowserDialog就是你想要的。
如果你只想要Ooki对话框中的文件夹浏览器,然后下载源代码,为文件夹浏览器挑选你需要的文件(提示:7个文件),它可以在。net 4.5.2中构建。我必须给System.Drawing添加一个引用。将原项目中的参考资料与你的进行比较。
如何确定哪些文件?在不同的Visual Studio实例中打开你的应用程序和Ookii。将VistaFolderBrowserDialog.cs添加到应用程序中,并继续添加文件,直到构建错误消失。你在Ookii项目中找到依赖项-控制-单击你想要跟踪到它的源(双关语)。
如果你懒得这么做,这里有你需要的文件……
NativeMethods.cs
SafeHandles.cs
VistaFolderBrowserDialog.cs
\ Interop
COMGuids.cs
ErrorHelper.cs
ShellComInterfaces.cs
ShellWrapperDefinitions.cs
编辑VistaFolderBrowserDialog.cs中的第197行,除非你想包含它们的资源。Resx
抛出新的InvalidOperationException(Properties.Resources.FolderBrowserDialogNoRootFolder);
throw new InvalidOperationException("Unable to retrieve the root folder.");
根据他们的license.txt将他们的版权声明添加到你的应用中
在\Ookii.Dialogs.Wpf.Sample\MainWindow.xaml.cs第160-169行代码是一个例子,你可以使用,但你需要删除这个,从MessageBox。显示(这个,用于WPF。
在我的机器上工作
我创建了一个UserControl,它是这样使用的:
<UtilitiesWPF:FolderEntry Text="{Binding Path=LogFolder}" Description="Folder for log files"/>
xaml源代码如下所示:
<UserControl x:Class="Utilities.WPF.FolderEntry"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<DockPanel>
<Button Margin="0" Padding="0" DockPanel.Dock="Right" Width="Auto" Click="BrowseFolder">...</Button>
<TextBox Height="Auto" HorizontalAlignment="Stretch" DockPanel.Dock="Right"
Text="{Binding Text, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}}" />
</DockPanel>
</UserControl>
还有隐藏代码
public partial class FolderEntry {
public static DependencyProperty TextProperty = DependencyProperty.Register("Text", typeof(string), typeof(FolderEntry), new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault));
public static DependencyProperty DescriptionProperty = DependencyProperty.Register("Description", typeof(string), typeof(FolderEntry), new PropertyMetadata(null));
public string Text { get { return GetValue(TextProperty) as string; } set { SetValue(TextProperty, value); }}
public string Description { get { return GetValue(DescriptionProperty) as string; } set { SetValue(DescriptionProperty, value); } }
public FolderEntry() { InitializeComponent(); }
private void BrowseFolder(object sender, RoutedEventArgs e) {
using (FolderBrowserDialog dlg = new FolderBrowserDialog()) {
dlg.Description = Description;
dlg.SelectedPath = Text;
dlg.ShowNewFolderButton = true;
DialogResult result = dlg.ShowDialog();
if (result == System.Windows.Forms.DialogResult.OK) {
Text = dlg.SelectedPath;
BindingExpression be = GetBindingExpression(TextProperty);
if (be != null)
be.UpdateSource();
}
}
}
}
这些答案都不适合我(通常是缺少参考资料或类似的东西)
但这很简单:
在WPF应用程序中使用FolderBrowserDialog
添加一个对System.Windows.Forms的引用,并使用以下代码:
var dialog = new System.Windows.Forms.FolderBrowserDialog();
System.Windows.Forms.DialogResult result = dialog.ShowDialog();
没有必要去寻找丢失的包裹。或者添加大量的类
这为我提供了一个现代化的文件夹选择器,还允许您创建一个新文件夹
我还没有看到部署到其他机器上的影响
推荐文章
- 在窗口上设置设计时间DataContext是一个编译器错误?
- 调用线程必须是STA,因为许多UI组件都要求这一点
- ContentControl和ContentPresenter有什么区别?
- WPF中控制模板和数据模板的区别
- 如何隐藏关闭按钮在WPF窗口?
- UI线程上的任务继续
- WPF数据绑定:我如何访问“父”数据上下文?
- 从System.Drawing.Bitmap中加载WPF BitmapImage
- 我应该使用什么MVVM框架?
- 在WPF中引入一个窗口到前面
- 窗口高度=“自动”未按预期工作
- 我如何得到一个动画gif在WPF工作?
- 如何在对话框中创建编辑文本框
- ListBox和ListView的区别是什么
- MVVM:从开始到结束的教程?