浏览WPF上的按钮

时间:2013-02-07 16:47:04

标签: c# wpf visual-studio-2012

在WPF中,通过使用c#,我希望用户能够将他们的数据导入gridview。所以我需要一个浏览按钮还是......

如果是,我该怎么做?

由于

1 个答案:

答案 0 :(得分:13)

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

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