我有一个 WPF、MVVM 应用程序。
我需要与 asp.net 中的“文件上传”控件相同的功能。
有人能告诉我如何实现吗?
<StackPanel Orientation="Horizontal">
<TextBox Width="150"></TextBox>
<Button Width="50" Content="Browse"></Button>
</StackPanel>
我有这个 xaml ......但是当你点击按钮时如何拥有那个“浏览窗口”?
最佳答案
您可以使用 OpenFileDialog 类来获取文件选择对话框
OpenFileDialog fileDialog= new OpenFileDialog();
fileDialog.DefaultExt = ".txt"; // Required file extension
fileDialog.Filter = "Text documents (.txt)|*.txt"; // Optional file extensions
fileDialog.ShowDialog();
阅读内容:您将从 OpenFileDialog 获取文件名,并使用它来执行 IO 操作。if (fileDialog.ShowDialog() == DialogResult.OK)
{
System.IO.StreamReader sr = new System.IO.StreamReader(fileDialog.FileName);
MessageBox.Show(sr.ReadToEnd());
sr.Close();
}
关于wpf - 在WPF中,如何实现一个文件上传控件(文本框和一个浏览文件的按钮)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4876833/