【问题标题】:How to use folderbrowser dialog box in wpf如何在 wpf 中使用文件夹浏览器对话框
【发布时间】:2020-05-14 18:37:34
【问题描述】:

我需要在我的 WPF 应用程序中添加 FolderBrowserDialog。 我知道一种方法是添加到System.Windows.Forms.dll,但这种方法无法正常工作。

我的 XAML:

<Window x:Class="Sample.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        WindowState="Normal"
        ResizeMode="NoResize"
        mc:Ignorable="d"
        Title="MainWindow" Height="600" Width="600">
    <Grid>
        <TextBox Height="30" width="80" Name="TextBox1"/>
        <Button Height="50" Width="50" Content="Browse" Click="btn_Click"/>
    </Grid>
</Window>

我的代码隐藏:

using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Sample
{
public partial class MainWindow : Window
    {

        public MainWindow()
        {
            InitializeComponent();
        }
        private void btn_Click(object sender, RoutedEventArgs e)
        {
            FolderBrowserDialog folderDialog = new FolderBrowserDialog();
            folderDialog.SelectedPath = "C:\\";

            DialogResult result = folderDialog.ShowDialog();
            if (result.ToString() == "OK") 
                textBox1.Text = folderDialog.SelectedPath;
         }
    }
}

我在依赖项中添加了System.Windows.Forms.dll。添加System.Windows.Forms.dll后,FolderBrowserDialog类工作正常,但InitializeComponent();报错:

InitializeComponent 在当前上下文中不存在

【问题讨论】:

  • 您必须完全限定 Winforms 类型,例如System.Windows.Forms.FolderBrowserDialog 。重要提示:FolderBrowserDialog 实现 IDisposable。因此,当使用FolderBrowserDialog 类型的局部变量时,请确保它在using 块(或finally)中使用。如果使用FolderBrowserDialog 类型的实例成员,那么也让周围的类型实现IDisposable

标签: c# wpf wpf-controls


【解决方案1】:

您需要从代码隐藏的顶部删除 using System.Windows.Forms; 并将其替换为 using System.Windows;

使用前一行会导致您的 Window 编译为 System.Windows.Forms.Window(WinForms 窗口),而不是 System.Window.Window(WPF 窗口。

然后您需要将完整的命名空间放入您的类型声明中,因此您的代码将变为:

private void btn_Click(object sender, RoutedEventArgs e)
{
    using (System.Windows.Forms.FolderBrowserDialog folderDialog = new System.Windows.Forms.FolderBrowserDialog())
    {
        folderDialog.SelectedPath = "C:\\";

        System.Windows.Forms.DialogResult result = folderDialog.ShowDialog();
        if (result.ToString() == "OK")
            textBox1.Text = folderDialog.SelectedPath;
    }
}

【讨论】:

  • 请注意,FolderBrowserDialog 实现了IDisposable
  • @BionicCode 很好,我添加了适当的using 块。
  • 我删除了 System.Windows.Forms,然后添加了 System.Windows.Forms.Window 但再次构建错误。错误 CS0234 命名空间“System.Windows.Forms”中不存在类型或命名空间名称“Window”(您是否缺少程序集引用?) FileOpenDialog C:\Users\SSC_03\source\repos\FileOpenDialog\FileOpenDialog\MainWindow。 xaml.cs 15 活动
  • @karthick19892089 你现在有代码System.Windows.Forms.Window 吗?因为你不应该。您可能需要编辑您的问题以显示您拥有的当前代码。
  • @karthick19892089 你搞砸了。只需删除using System.Windows.Forms不要在此处添加任何其他内容。因为您添加了System.Windows.Forms.Window,所以您使用System.Windows 命名空间创建了歧义,因为它们都包含Window 类。 WPF 需要引用System.Windows.Windows。没有人告诉你添加这个命名空间。这个答案很完美。使用此解决方案。无需导入任何第三方库,如 FolderBrowserDialogEx。请注意,这不是 .NET 库。在这一点上这是一个不必要的依赖。只需使用此答案中所示的 .NET 对话框即可。
【解决方案2】:

WPF 没有本机 FolderBrowserDialog 控件。莫名其妙,我知道。

您可以使用如上图所示的 winforms 版本,也可以尝试使用 ookii.dialogs 之类的 3rd 方控件

http://www.ookii.org/software/dialogs/

【讨论】:

  • 谢谢。如何与wpf项目集成
【解决方案3】:

我得到了答案。当我在 wpf 中使用文件夹浏览器对话框时。我需要在 ManageNuGetPackage 中下载 FolderBrowserDialogEx.dll。下载包后。 FolderBrowser对话框工作正常。试试这个。

【讨论】:

  • @Keith Stein 的答案是完美的。使用此解决方案。无需导入任何第三方库,如 FolderBrowserDialogEx。请注意,这不是 .NET 库。在这一点上这是一个不必要的依赖。只需使用 .NET 对话框,如Keith's 答案所示。
【解决方案4】:

试试这些 using 语句:

using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using FolderBrowserDialog = System.Windows.Forms.FolderBrowserDialog; 

试试这个作为你的课程

using System;
using System.Text;
using System.Threading.Tasks;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Input;
using FolderBrowserDialog = System.Windows.Forms.FolderBrowserDialog;

namespace Sample
{
     public partial class MainWindow : Window
     {
          public MainWindow()
          {
               InitializeComponent();
          }

          public string FolderPath { get; set; } = string.Empty;

          public string OpenFolderTitle { get; set; } = string.Empty;

          private void btn_Click(object sender, RoutedEventArgs e)
          {
               ShowFolderPathEditWindow();
          }

          private void ShowFolderPathEditWindow()
          {
               string defaultFolderPath = string.IsNullOrEmpty(FolderPath) ? Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) : FolderPath;
               string folderPath = ShowFolderBrowserDialog(defaultFolderPath);
               bool TFfolderPath = string.IsNullOrEmpty(folderPath) | string.IsNullOrWhiteSpace;

               FolderPath = TFfolderPath switch { true => string.Empty, false => folderPath };
          }

          private string ShowFolderBrowserDialog(string defaultFolderPath)
          {
               FolderBrowserDialog folderBrowserDialog = new FolderBrowserDialog();

               folderBrowserDialog.Description = OpenFolderTitle;
               folderBrowserDialog.ShowNewFolderButton = true;
               folderBrowserDialog.SelectedPath = defaultFolderPath;
               folderBrowserDialog.ShowDialog();
               return folderBrowserDialog.SelectedPath;
          }
     }
}

如果要控制文件夹位置,可以将 TextBox 的 Text 属性绑定到 FolderPath。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-29
    • 1970-01-01
    • 1970-01-01
    • 2010-11-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多