0
点赞
收藏
分享

微信扫一扫

什么是 WPF 中的转换器?如何自定义一个值转换器?

天使魔鬼 2024-11-18 阅读 5
wpfC#

在进行WPF开发时,System.Windows.Forms.FolderBrowserDialog的选择文件夹功能不支持输入路径:

在这里插入图片描述

希望能够获得下图所示的选择文件夹功能:

在这里插入图片描述

于是,通过NuGet中安装Ookii.Dialogs.Wpf包,并创建一个简单的工具类:

using Ookii.Dialogs.Wpf;
using System.Windows.Forms;

namespace Utils
{
    public class DialogUtil
    {
        public static bool GetDir(string description, out string dir, string rootDir = null)
        {
            dir = null;
            if (VistaFolderBrowserDialog.IsVistaFolderDialogSupported)
            {
                VistaFolderBrowserDialog val = new VistaFolderBrowserDialog();
                val.Description = description;

                val.SelectedPath = rootDir;
                val.Multiselect = (false);
                if (val.ShowDialog() == true)
                {
                    dir = val.SelectedPath;
                    return true;
                }
            }
            else
            {
                FolderBrowserDialog folderBrowserDialog = new FolderBrowserDialog();
                folderBrowserDialog.Description = description;
                if (folderBrowserDialog.ShowDialog() != System.Windows.Forms.DialogResult.OK)
                    return false;
                dir = folderBrowserDialog.SelectedPath;
                return true;
            }
            return false;
        }
    }
}

调用该工具类:

if (DialogUtil.GetDir("选择文件夹", out string dir))
{
	//to do
}
举报

相关推荐

0 条评论