WPF本身并没有为我们提供文件浏览的控件, 也不能直接使用Forms中的控件,而文件浏览对话框又是我们最常用的控件之一. 下面是我实现的方式
使用Forms中的OpenFileDialog控件
WPF中是不能直接使用Forms中的控件的,否则会提示找不到Forms名字控件. 如果你仍然要用, 答案便是添加.net 引用reference
之后就可以像下面一样正常使用Forms中的控件了
1
2
3
4
5
6
7
8
9
|
System.Windows.Forms.OpenFileDialog openFileDialog1 =
new
System.Windows.Forms.OpenFileDialog();
openFileDialog1.InitialDirectory =
"c:\\"
;
openFileDialog1.Filter =
"txt files (*.txt)|*.txt|All files (*.*)|*.*"
;
openFileDialog1.FilterIndex = 2;
openFileDialog1.RestoreDirectory =
true
;
if
(openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
//此处做你想做的事 ...=openFileDialog1.FileName;
}
|
类似的有文件夹浏览对话框:
1
2
3
4
5
6
|
System.Windows.Forms.FolderBrowserDialog folderBrowserDialog =
new
System.Windows.Forms.FolderBrowserDialog();
System.Windows.Forms.DialogResult result = folderBrowserDialog.ShowDialog();
if
(result == System.Windows.Forms.DialogResult.OK)
{
tb_FolderPath.Text = folderBrowserDialog.SelectedPath;
}
|