文件对话框WPF(5)----文件浏览对话框
废话就不多说了,开始。。。
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)----文件浏览对话框的更多相关文章
- WPF中使用文件浏览对话框的几种方式
原文:WPF中使用文件浏览对话框的几种方式 WPF本身并没有为我们提供文件浏览的控件, 也不能直接使用Forms中的控件,而文件浏览对话框又是我们最常用的控件之一. 下面是我实现的方式 方式1: 使用 ...
- FolderBrowserDialog(文件夹浏览对话框)
1.选择数据库目录,在此处不需要新建文件夹,因此屏蔽新建文件夹按钮. C#代码 FolderBrowserDialog df = new FolderBrowserDialog(); //设置文件浏览 ...
- WPF选择文件、文件夹和另存为对话框
WPF提供了选择文件对话框,但并没有提供选择文件夹的对话框. OpenFileDialog类存在于PresentationFramework.dll程序集. public string SelectF ...
- WPF 打开文件 打开路径对话框
WPF调用WinForm中的 OpenFileDialog 和 FolderBrowserDialog 来实现响应的功能 对应的引用程序集: using System.Windows.Forms; O ...
- WPFの操作文件浏览框几种方式
方式1: 使用win32控件OpenFileDialog Microsoft.Win32.OpenFileDialog ofd = new Microsoft.Win32.OpenFileDialog ...
- VC++打开对话框选择一个文件夹路径 BROWSEINFO结构
typedef struct _browseinfoW { HWND hwndOwner; PCIDLIST_ABSOLUTE pidlRoot; LPWSTR pszDisplayName; // ...
- qt——QFileDialog使用对话框选取本地文件
QT在学习的过程中总是遇到各种问题,没有人解答,只有自己在研究并且在网上搜索一些资料,从初学到现在入门,一直都是这样走过来的,虽然走得很艰难,但是每一个阶段都会有所收获,最近在做一个图片浏览模块的功能 ...
- MFC 打开文件对话框 打开单个文件
CFileDialog的语法: CFileDialog(BOOL bOpenFileDialog,LPCTSTR lpszDefExt=NULL,LPCTSTR lpszFileName=NULL,D ...
- Android——用对话框做登陆界面(自定义对话框AlertDialog,多线程,进度条ProgressDialog,ListView,GridView,SharedPreferences存,读数据,存取文本,assets文件)
效果: 1.点击图标进入页面二 2.页面2图片暂停显示5秒进入页面三 3.点击页面三登陆按钮,打开登陆对话框,输入密码进入页面四 点击下载按钮,显示水平进度条 点击保存和获取用户名和密码 进入页面六 ...
随机推荐
- a链接bug
<a href=”javascript:void(0);” class="btn" id="btn"></a> 在ios系统中(andr ...
- Appium Server 源码分析之启动运行Express http服务器
通过上一个系列Appium Android Bootstrap源码分析我们了解到了appium在安卓目标机器上是如何通过bootstrap这个服务来接收appium从pc端发送过来的命令,并最终使用u ...
- changePage() 页面跳转
jQuery.mobile.changePage( to [, options ] ) 从一个页面跳转到另一个页面,使用$.mobile对象的changePage方法来实现.但要使用此方式的时候,要以 ...
- Python 2.7.3 urllib2.urlopen 获取网页出现乱码解决方案
出现乱码的原因是,网页服务端有bug,它硬性使用使用某种特定的编码方案,而并没有按照客户端的请求头的编码要求来发送编码. 解决方案:使用chardet来猜测网页编码. 1.去chardet官网下载ch ...
- Your build host version of Xamarin.IOS (release NO.)is too recent to work with the IOS designer
Encounted such error in VS after I update the xamarin at Mac side.Here is the solution for u to refe ...
- 使用rem设计移动端自适应页面一(转载)
1.困扰多时的问题 在这之前做Web App开发的的时候,在自适应方面一般都是宽度通过百分比,高度以iPhone6跟iPhone5之间的一个平衡值写死,我们的设计稿都是iPhone5的640 * 11 ...
- 12个很少被人知道的CSS事实
之前没有认真的研究过,padding-bottom的值如果是百分比,那么它的实际值是根据父类的宽度来调整的.我还以为是根据这个元素的本身的宽度来定义呢?汗..padding-top/padding-l ...
- DDD实践(一)
DDD实践切入点(一) 前两篇:大型系统的支撑,应用系统开发思想的变迁 之前大致说了使用DDD的前期准备,现在可以真正开始实践了,以我刚刚结束的一个简单的经典DDD方式的项目为例子,当然由于比较简单, ...
- Date的使用
方法 说明 Date() 返回当日的日期和时间 getDate() 获取当天(1-31) getDay() 获取当天的星期(0-6) getMonth() 获取月份(0-11) getFullYear ...
- SpringMVC中Controller
详解SpringMVC中Controller的方法中参数的工作原理[附带源码分析] 目录 前言 现象 源码分析 HandlerMethodArgumentResolver与HandlerMethodR ...