WPF上的浏览按钮

本文关键字:按钮 浏览 WPF | 更新日期: 2023-09-27 17:58:07

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

如果是,我该怎么做?

WPF上的浏览按钮

下面的代码帮助您显示浏览按钮

<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;
 }