WPF 上的浏览​​按钮

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14756414/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 07:24:24  来源:igfitidea点击:

Browse button on WPF

c#wpfvisual-studio-2012

提问by hslldm

In a WPF application, by using c#, I want users to be able to import their data to grid view. So do i need a browse button or something?

在 WPF 应用程序中,通过使用 c#,我希望用户能够将他们的数据导入到网格视图中。那么我需要一个浏览按钮还是什么?

If yes how can I do that?

如果是,我该怎么做?

回答by Smaug

The below code helps you to display the browse button

以下代码可帮助您显示浏览按钮

<TextBox Height="32" HorizontalAlignment="Left" Margin="6,10,0,0" Name="FileNameTextBox"
                 VerticalAlignment="Top" Width="393" />
        <Button Content="Browse" Height="32" HorizontalAlignment="Left" Margin="405,10,0,0"
                Name="button1" VerticalAlignment="Top" Width="88" Click="button1_Click" />


// Create OpenFileDialog
Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();          

// Set filter for file extension and default file extension
dlg.DefaultExt = ".txt";
dlg.Filter = "Text documents (.txt)|*.txt";

// Display OpenFileDialog by calling ShowDialog method
Nullable<bool> result = dlg.ShowDialog();

// Get the selected file name and display in a TextBox
if (result == true)
{
    // Open document
    string filename = dlg.FileName;
    FileNameTextBox.Text = filename;
 }