废话就不多说了,开始。。。

WPF中文件浏览对话框的实现可以利用Windows API Code Pack,它是一个用于访问Windows Vista/7 特性的托管代码函数库,但并没有包含在.NET 4.0中。

该代码包的特性如下所示:

  • 支撑Windows Shell命名空间对象,包含新的Windows 7资源库(Libraries)、固定名称文件夹和非文件系统容器。
  • Windows Vista和Windows 7任务对话框(Task Dialogs)。
  • 支撑WPF和Windows Forms的Windows 7资源管理器浏览器控件(Explorer Browser Control)。
  • 支撑Shell的属性系统。
  • 用于Windows 7任务栏Jumplists、Icon Overlay和Progress Bar的帮助程序。
  • 支撑Windows Vista和Windows 7的通用文件对话框,并包含了自定义文件对话框控件。
  • 支撑Direct3D 11.0和DXGI 1.0/1.1的API。
  • 传感器平台(Sensor Platform)API
  • 扩展的语言服务(Extended Linguistic Services)API。

1:代码包下载之后,解压,将其中的Microsoft.WindowsAPICodePack.dll 和Microsoft.WindowsAPICodePack.Shell.dll拷贝至工程中。然后Reference-->Add将其添加至Project中的References。

2:代码编写时,将其导入命名空间:

using Microsoft.WindowsAPICodePack.Shell;
using Microsoft.WindowsAPICodePack.Dialogs;

3:前台xmal代码如下:

<Window x:Class="WpfFileExploerDialog.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="148" Width="434" Background="#E609072F">
<Grid Name="Grid1">
<TextBox Height="25" Text = "{Binding Path=TextBoxValue}" HorizontalAlignment="Left" Margin="15,29,0,0" Name="textBoxFilePath" VerticalAlignment="Top" Width="347" />
<Button Content="..." Click="ButtonFileSelect" Height="24" HorizontalAlignment="Left" Margin="377,30,0,0" Name="buttonFileDialog" VerticalAlignment="Top" Width="25" />
</Grid>
</Window>

4:后台xmal.cs代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes; using System.ComponentModel;
using Microsoft.WindowsAPICodePack.Shell;
using Microsoft.WindowsAPICodePack.Dialogs; namespace WpfFileExploerDialog
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window, INotifyPropertyChanged
{
public MainWindow()
{
InitializeComponent();
Grid1.DataContext = this;
} public event PropertyChangedEventHandler PropertyChanged; protected void NotifyPropertyChanged(string property)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(property));
}
} private string _Value2; public string TextBoxValue
{
get { return _Value2; }
set
{
if (value != _Value2)
{
_Value2 = value;
NotifyPropertyChanged("TextBoxValue");
}
}
} private void ButtonFileSelect(object sender, RoutedEventArgs e)
{
ShellContainer selectedFolder = null;
selectedFolder = KnownFolders.Computer as ShellContainer;
CommonOpenFileDialog commonOpenFileDialog = new CommonOpenFileDialog();
commonOpenFileDialog.InitialDirectoryShellContainer = selectedFolder;
commonOpenFileDialog.EnsureReadOnly = true; if (commonOpenFileDialog.ShowDialog() == CommonFileDialogResult.Ok)
{
TextBoxValue = commonOpenFileDialog.FileName;
}
}
}
}
    每日一道理
如果说生命是一座庄严的城堡,如果说生命是一株苍茂的大树,如果说生命是一只飞翔的海鸟。那么,信念就是那穹顶的梁柱,就是那深扎的树根,就是那扇动的翅膀。没有信念,生命的动力便荡然无存;没有信念,生命的美丽便杳然西去。(划线处可以换其他词语)

5:程序运行结果如下:

另外,还可以将文件浏览窗口直接定位到固定的文件夹,并且添加想要的文件过滤器,例如上面的代码就是将其定位到SampleVideos文件夹:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes; using System.ComponentModel;  
using Microsoft.WindowsAPICodePack.Shell;
using Microsoft.WindowsAPICodePack.Dialogs; namespace WpfFileExploerDialog
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window, INotifyPropertyChanged  
    {
        public MainWindow()
        {
            InitializeComponent();
            Grid1.DataContext = this;
        }         public event PropertyChangedEventHandler PropertyChanged;         protected void NotifyPropertyChanged(string property)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(property));
            }
        }         private string _Value2;         public string TextBoxValue
        {
            get { return _Value2; }
            set
            {
                if (value != _Value2)
                {
                    _Value2 = value;
                    NotifyPropertyChanged("TextBoxValue");
                }
            }
        }           private void ButtonFileSelect(object sender, RoutedEventArgs e)
        {
            ShellContainer selectedFolder = null;             //文件夹定位至SampleVideos
            selectedFolder = KnownFolders.SampleVideos as ShellContainer;
            CommonOpenFileDialog commonOpenFileDialog = new CommonOpenFileDialog();
            commonOpenFileDialog.InitialDirectoryShellContainer = selectedFolder;
            commonOpenFileDialog.EnsureReadOnly = true;             //设置文件过滤
            commonOpenFileDialog.Filters.Add(new CommonFileDialogFilter("WMV Files", "*.wmv"));
            commonOpenFileDialog.Filters.Add(new CommonFileDialogFilter("AVI Files", "*.avi"));
            commonOpenFileDialog.Filters.Add(new CommonFileDialogFilter("MP3 Files", "*.mp3"));
            commonOpenFileDialog.Filters.Add(new CommonFileDialogFilter("MKV Files", "*.mkv"));             if (commonOpenFileDialog.ShowDialog() == CommonFileDialogResult.Ok)
            {
                TextBoxValue = commonOpenFileDialog.FileName;
            }
        }
    }
}

文章结束给大家分享下程序员的一些笑话语录:

看新闻说中国输入法全球第一!领先了又如何?西方文字根本不需要输入法。一点可比性都没有。

---------------------------------
原创文章 By
文件和对话框
---------------------------------

文件对话框WPF(5)----文件浏览对话框的更多相关文章

  1. WPF中使用文件浏览对话框的几种方式

    原文:WPF中使用文件浏览对话框的几种方式 WPF本身并没有为我们提供文件浏览的控件, 也不能直接使用Forms中的控件,而文件浏览对话框又是我们最常用的控件之一. 下面是我实现的方式 方式1: 使用 ...

  2. FolderBrowserDialog(文件夹浏览对话框)

    1.选择数据库目录,在此处不需要新建文件夹,因此屏蔽新建文件夹按钮. C#代码 FolderBrowserDialog df = new FolderBrowserDialog(); //设置文件浏览 ...

  3. WPF选择文件、文件夹和另存为对话框

    WPF提供了选择文件对话框,但并没有提供选择文件夹的对话框. OpenFileDialog类存在于PresentationFramework.dll程序集. public string SelectF ...

  4. WPF 打开文件 打开路径对话框

    WPF调用WinForm中的 OpenFileDialog 和 FolderBrowserDialog 来实现响应的功能 对应的引用程序集: using System.Windows.Forms; O ...

  5. WPFの操作文件浏览框几种方式

    方式1: 使用win32控件OpenFileDialog Microsoft.Win32.OpenFileDialog ofd = new Microsoft.Win32.OpenFileDialog ...

  6. VC++打开对话框选择一个文件夹路径 BROWSEINFO结构

    typedef struct _browseinfoW { HWND hwndOwner; PCIDLIST_ABSOLUTE pidlRoot; LPWSTR pszDisplayName; // ...

  7. qt——QFileDialog使用对话框选取本地文件

    QT在学习的过程中总是遇到各种问题,没有人解答,只有自己在研究并且在网上搜索一些资料,从初学到现在入门,一直都是这样走过来的,虽然走得很艰难,但是每一个阶段都会有所收获,最近在做一个图片浏览模块的功能 ...

  8. MFC 打开文件对话框 打开单个文件

    CFileDialog的语法: CFileDialog(BOOL bOpenFileDialog,LPCTSTR lpszDefExt=NULL,LPCTSTR lpszFileName=NULL,D ...

  9. Android——用对话框做登陆界面(自定义对话框AlertDialog,多线程,进度条ProgressDialog,ListView,GridView,SharedPreferences存,读数据,存取文本,assets文件)

    效果: 1.点击图标进入页面二 2.页面2图片暂停显示5秒进入页面三 3.点击页面三登陆按钮,打开登陆对话框,输入密码进入页面四 点击下载按钮,显示水平进度条 点击保存和获取用户名和密码 进入页面六  ...

随机推荐

  1. 深入理解JavaScript(1)

    才华横溢的Stoyan Stefanov,在他写的由O’Reilly初版的新书<JavaScript Patterns>(JavaScript模式)中,我想要是为我们的读者贡献其摘要,那会 ...

  2. php操作xml并插入到数据库中

    php操作xml并插入到数据库中 <? php header('content-type:text/html;charset=utf-8'); mysql_connect('localhost' ...

  3. WIN7远程桌面重启、关机

    在使用远程桌面访问Win7系统时会发现一个小问题,在xp远程桌面中存在的重启和关机菜单在win7远程桌面中不见了,如图: 这也给我们的使用带来了一些小小的麻烦,但实际上微软依然保留了命令行的方式来实现 ...

  4. C# TextBox实现全选

    A. 设置全局变量: 1.定义了个全局变量放本次点击的textbox的名字,默认为空. 2.textbox的Enter事件里SelectAll()一下 3.Click事件里判断全局变量是否是该text ...

  5. C语言缓冲区清空

    C语言中有几个基本输入函数: //获取字符系列 int fgetc(FILE *stream); int getc(FILE *stream); int getchar(void); //获取行系列 ...

  6. ubuntu下的apache的虚拟主机的配置

    ubuntu下的虚拟主机的配置相对window下的虚拟主机配置有些许不同. 对于要新建的虚拟主机,我们可以有如下几个步骤: 1.在/etc/apache2/sites-available/目录下新建一 ...

  7. C#实现文档转换成PDF

    网上有很多将doc.ppt.xls等类型的文档转换成pdf的方法,目前了解到的有两大类: 1.使用虚拟打印机将doc.ppt.xls等类型的文档 2.使用OFFICE COM组件 我采用了第二种方法实 ...

  8. 【jar包】Android——eclipse共享library以及导出jar包

    android的apk在在eclipse上进行开发的时候,有时候需要import其它包中的一些class,正常的方法就是在java build path中library 中添加 jar 包! 转载注明 ...

  9. Bootstrap+Knockout.JS+ASP.Net MVC3+PetaPOCO实现CRUD操作

    Bootstrap+Knockout.JS+ASP.Net MVC3+PetaPOCO实现CRUD操作 1.需求: 1.1)页面要美观大气 1.2)前端代码要简洁清晰,要用MVC或是MVVM框架 1. ...

  10. 关于覆盖Object中的hashCode, equals和toString

    最近在看<Effective Java>,里面看到了关于重载hashCode.equals和toString方法的篇章,顿时觉得视野开拓了不少,而且正结合自己工作.项目中的实例,觉得有必要 ...