In WPF, how to implement a file upload control (text box and button for viewing a file)?

I have a WPF application, MVVM.

I need the same functionality as the file upload control in asp.net.

Can someone tell me how to implement this?

 <StackPanel Orientation="Horizontal">
                <TextBox Width="150"></TextBox>
                <Button Width="50" Content="Browse"></Button>
</StackPanel>

I have this xaml ... but how to get this "viewport" when a button is clicked?

+3
source share
2 answers

You can use the OpenFileDialog class to select a file selection dialog

OpenFileDialog fileDialog= new OpenFileDialog(); 
fileDialog.DefaultExt = ".txt"; // Required file extension 
fileDialog.Filter = "Text documents (.txt)|*.txt"; // Optional file extensions

fileDialog.ShowDialog(); 

To read the contents: you get the file name from OpenFileDialog and use it to perform an I / O operation.

 if(fileDialog.ShowDialog() == DialogResult.OK)
  {
     System.IO.StreamReader sr = new 
     System.IO.StreamReader(fileDialog.FileName);
     MessageBox.Show(sr.ReadToEnd());
     sr.Close();
  }
+7
source
<StackPanel Orientation="Horizontal">
     <TextBox Width="150"></TextBox>
     <Button Width="50" Content="Browse" Command="{Binding Path=CommandInViewModel}"></Button>
</StackPanel>

, . , . . , , .

+2

Source: https://habr.com/ru/post/1789535/


All Articles