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:代码编写时,将其导入命名空间:
[css] view plain copy
01.using Microsoft.WindowsAPICodePack.Shell;
02.using Microsoft.WindowsAPICodePack.Dialogs;
3:前台xmal代码如下
<Window x:Class="WpfFileExploerDialog.MainWindow"
02. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
03. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
04. Title="MainWindow" Height="148" Width="434" Background="#E609072F">
05. <Grid Name="Grid1">
06. <TextBox Height="25" Text = "{Binding Path=TextBoxValue}" HorizontalAlignment="Left" Margin="15,29,0,0" Name="textBoxFilePath" VerticalAlignment="Top" Width="347" />
07. <Button Content="..." Click="ButtonFileSelect" Height="24" HorizontalAlignment="Left" Margin="377,30,0,0" Name="buttonFileDialog" VerticalAlignment="Top" Width="25" />
08. </Grid>
09.</Window>
4:后台xmal.cs代码如下:
using System;
02.using System.Collections.Generic;
03.using System.Linq;
04.using System.Text;
05.using System.Windows;
06.using System.Windows.Controls;
07.using System.Windows.Data;
08.using System.Windows.Documents;
09.using System.Windows.Input;
10.using System.Windows.Media;
11.using System.Windows.Media.Imaging;
12.using System.Windows.Navigation;
13.using System.Windows.Shapes;
14.
15.using System.ComponentModel;
16.using Microsoft.WindowsAPICodePack.Shell;
17.using Microsoft.WindowsAPICodePack.Dialogs;
18.
19.namespace WpfFileExploerDialog
20.{
21. /// <summary>
22. /// Interaction logic for MainWindow.xaml
23. /// </summary>
24. public partial class MainWindow : Window, INotifyPropertyChanged
25. {
26. public MainWindow()
27. {
28. InitializeComponent();
29. Grid1.DataContext = this;
30. }
31.
32.
33. public event PropertyChangedEventHandler PropertyChanged;
34.
35. protected void NotifyPropertyChanged(string property)
36. {
37. if (PropertyChanged != null)
38. {
39. PropertyChanged(this, new PropertyChangedEventArgs(property));
40. }
41. }
42.
43. private string _Value2;
44.
45. public string TextBoxValue
46. {
47. get { return _Value2; }
48. set
49. {
50. if (value != _Value2)
51. {
52. _Value2 = value;
53. NotifyPropertyChanged("TextBoxValue");
54. }
55. }
56. }
57.
58. private void ButtonFileSelect(object sender, RoutedEventArgs e)
59. {
60. ShellContainer selectedFolder = null;
61. selectedFolder = KnownFolders.Computer as ShellContainer;
62. CommonOpenFileDialog commonOpenFileDialog = new CommonOpenFileDialog();
63. commonOpenFileDialog.InitialDirectoryShellContainer = selectedFolder;
64. commonOpenFileDialog.EnsureReadOnly = true;
65.
66.
67. if (commonOpenFileDialog.ShowDialog() == CommonFileDialogResult.Ok)
68. {
69. TextBoxValue = commonOpenFileDialog.FileName;
70. }
71. }
72. }
73.}
5:程序运行结果如下:
另外,还可以将文件浏览窗口直接定位到固定的文件夹,并且添加想要的文件过滤器,例如下面的代码就是将其定位到SampleVideos文件夹:
using System;
02.using System.Collections.Generic;
03.using System.Linq;
04.using System.Text;
05.using System.Windows;
06.using System.Windows.Controls;
07.using System.Windows.Data;
08.using System.Windows.Documents;
09.using System.Windows.Input;
10.using System.Windows.Media;
11.using System.Windows.Media.Imaging;
12.using System.Windows.Navigation;
13.using System.Windows.Shapes;
14.
15.using System.ComponentModel;
16.using Microsoft.WindowsAPICodePack.Shell;
17.using Microsoft.WindowsAPICodePack.Dialogs;
18.
19.namespace WpfFileExploerDialog
20.{
21. /// <summary>
22. /// Interaction logic for MainWindow.xaml
23. /// </summary>
24. public partial class MainWindow : Window, INotifyPropertyChanged
25. {
26. public MainWindow()
27. {
28. InitializeComponent();
29. Grid1.DataContext = this;
30. }
31.
32.
33. public event PropertyChangedEventHandler PropertyChanged;
34.
35. protected void NotifyPropertyChanged(string property)
36. {
37. if (PropertyChanged != null)
38. {
39. PropertyChanged(this, new PropertyChangedEventArgs(property));
40. }
41. }
42.
43. private string _Value2;
44.
45. public string TextBoxValue
46. {
47. get { return _Value2; }
48. set
49. {
50. if (value != _Value2)
51. {
52. _Value2 = value;
53. NotifyPropertyChanged("TextBoxValue");
54. }
55. }
56. }
57.
58. private void ButtonFileSelect(object sender, RoutedEventArgs e)
59. {
60. ShellContainer selectedFolder = null;
61.
62. //文件夹定位至SampleVideos
63. selectedFolder = KnownFolders.SampleVideos as ShellContainer;
64. CommonOpenFileDialog commonOpenFileDialog = new CommonOpenFileDialog();
65. commonOpenFileDialog.InitialDirectoryShellContainer = selectedFolder;
66. commonOpenFileDialog.EnsureReadOnly = true;
67.
68. //设置文件过滤
69. commonOpenFileDialog.Filters.Add(new CommonFileDialogFilter("WMV Files", "*.wmv"));
70. commonOpenFileDialog.Filters.Add(new CommonFileDialogFilter("AVI Files", "*.avi"));
71. commonOpenFileDialog.Filters.Add(new CommonFileDialogFilter("MP3 Files", "*.mp3"));
72. commonOpenFileDialog.Filters.Add(new CommonFileDialogFilter("MKV Files", "*.mkv"));
73.
74. if (commonOpenFileDialog.ShowDialog() == CommonFileDialogResult.Ok)
75. {
76. TextBoxValue = commonOpenFileDialog.FileName;
77. }
78. }
79. }
80.}