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

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. POJ 2553 The Bottom of a Graph (强连通分量)

    题目地址:POJ 2553 题目意思不好理解.题意是:G图中从v可达的全部点w,也都能够达到v,这种v称为sink.然后升序输出全部的sink. 对于一个强连通分量来说,全部的点都符合这一条件,可是假 ...

  2. SQL点滴29—错误无处不在

    原文:SQL点滴29-错误无处不在 我只想说以下是很基础的sql知识,但是很容易犯错.所以睁大我们的眼睛,屏住我们的呼吸,小心的检查吧! 案例1if not exists (select OrderI ...

  3. Java 多并发之原子访问(Atomic Access)

    在编程中,一个原子操作是只会出现一次的.一个原子操作在中间不会停止:要么全部发生要么一点也不发生.我们只有在原子操作完成之后才会看到原子操作的具体影响. 甚至是非常简单的表达式能够构造分解为简单操作的 ...

  4. PDF解决方案(2)--文件转PDF

    相关专题链接: PDF解决方案(1)--文件上传 PDF解决方案(2)--文件转PDF PDF解决方案(3)--PDF转SWF PDF解决方案(4)--在线浏览 前言:上一篇中讲到的文件上传,文件上传 ...

  5. ubuntu下的词典的安装

    因为从事开发,安装一个词典是很有必要,文中介绍安装openyoudao和stardic两个软件的方法 一.openyoudao的安装 因为是由window转来学ubuntu的,所以总是想安装和wind ...

  6. Get Resultset from Oracle Stored procedure

    http://stackoverflow.com/questions/1170548/get-resultset-from-oracle-stored-procedure

  7. javascript 学习总结(四)Date对象

    1.Date.now() //Date.now() is in ECMAScript 5 //Prior to that, use +new Date() //获取当前时间 var now = (ty ...

  8. 使用rem设计移动端自适应页面三(转载)

    使用rem 然后根据媒体查询实现自适应.跟使用JS来自适应也是同个道理,不过是js更精确一点.使用媒体查询: html { font-size: 62.5% } @media only screen ...

  9. Android项目--Json解析

    在过去的一段时间里,我希望做一个天气的应用,但是由于老版的天气接口已经不能用了.只能更新到2014年3月4日. 不过有些东西,哪来学习一下,也是可以的. 比如:http://m.weather.com ...

  10. Javascript实例技巧精选(6)—滚动鼠标中键读取Json数据分页显示网页内容

    >>点击这里下载完整html源码<< 截图如下: 滚动鼠标中键读取Json数据分页显示网页内容,关键的Javascript如下: <script type="t ...