文件对话框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.点击页面三登陆按钮,打开登陆对话框,输入密码进入页面四 点击下载按钮,显示水平进度条 点击保存和获取用户名和密码 进入页面六 ...
随机推荐
- 查看oracle数据库服务器的名字
原文:查看oracle数据库服务器的名字 windows 中 1. select name from v$database ; 直接运行就可以查看了, 2.查看tnsnames.ora 的连接,有个S ...
- Cts分析框架(4)-添加任务
Debug watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvaXRmb290YmFsbA==/font/5a6L5L2T/fontsize/400/fill/ ...
- 快速构建Windows 8风格应用5-ListView数据控件
原文:快速构建Windows 8风格应用5-ListView数据控件 本篇博文主要介绍什么是ListView数据控件.如何构建ListView数据控件. 什么是ListView数据控件? 1) Li ...
- ASP.NET 5 Overview
ASP.NET 5概观 (ASP.NET 5 Overview) http://www.asp.net/vnext/overview/aspnet-vnext/aspnet-5-overview AS ...
- sbt公布assembly解决jar包冲突 deduplicate: different file contents found in the following
一个.问题定义 近期使用sbt战斗assembly发生故障时,包,在package什么时候,发生jar包冲突/文件冲突,两个相同class来自不同jar包classpath内心冲突. 有关详细信息:我 ...
- cocos2d-x3.2中将XCode发展project转移到VS2010可能会发生错误
一些代码在线xcode写.我们希望我们自己的屌丝vs上述的实施,要重新构建它project,然后加载.但是绝对 没想到在VS里新建project再加入文件,编译后出现了好多错误.以下就把解决这些错误的 ...
- 题目要求:建立一个类Str,将一个正整数转换成相应的字符串,例如整数3456转换为字符串"3456".
题目要求:建立一个类Str,将一个正整数转换成相应的字符串,例如整数3456转换为字符串"3456". 关键:怎么将一个数字转换为字符? [cpp] view plaincopy ...
- slice、substring、substr的区别
首先它们都接收两个参数,slice和substring接收的是起始位置与结束位置,而substr接收的是起始位置和所要截取的字符长度. 特殊注意: 当第二参数大于第一个参数时,slice会返回空字 ...
- POCO Controller
---恢复内容开始--- POCO Controller 你这么厉害,ASP.NET vNext 知道吗? 写在前面 阅读目录: POCO 是什么? 为什么会有 POJO? POJO 的意义 PO ...
- JAVA 作业:图形界面
自己动手写的一个小JAVA 程序: 一个学生管理小系统,虽然很挫,但是这我学JAVA的第一步.学了2天JAVA没有白费! import java.awt.*; import java.awt.even ...