在WPF中,如何实现文件上传控件(文本框和浏览文件的按钮)? [英] In WPF, how to implement a file upload control (textbox and a button to browse file)?
本文介绍了在WPF中,如何实现文件上传控件(文本框和浏览文件的按钮)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个WPF,MVVM应用程序.
I have a WPF,MVVM application.
我需要与asp.net中的文件上传"控件相同的功能.
I need the functionality same as "File Upload" control in asp.net.
有人可以告诉我如何实现吗?
Can somebody tell me how to implement that ?
<StackPanel Orientation="Horizontal">
<TextBox Width="150"></TextBox>
<Button Width="50" Content="Browse"></Button>
</StackPanel>
我有这个xaml ...但是当您单击按钮时如何拥有该浏览窗口"?
I have this xaml...but how to have that "browse window" when you click button ?
推荐答案
您可以使用OpenFileDialog类获取文件选择对话框
You can use OpenFileDialog class to get a file choose dialog
OpenFileDialog fileDialog= new OpenFileDialog();
fileDialog.DefaultExt = ".txt"; // Required file extension
fileDialog.Filter = "Text documents (.txt)|*.txt"; // Optional file extensions
fileDialog.ShowDialog();
要读取内容:您将从OpenFileDialog中获取文件名,然后使用该文件名执行IO操作.
To read the content : You will get the filename from the OpenFileDialog and use that to do what IO operation on it.
if(fileDialog.ShowDialog() == DialogResult.OK)
{
System.IO.StreamReader sr = new
System.IO.StreamReader(fileDialog.FileName);
MessageBox.Show(sr.ReadToEnd());
sr.Close();
}
这篇关于在WPF中,如何实现文件上传控件(文本框和浏览文件的按钮)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文