Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where's the file-picker dialog in WPF?

Tags:

c#

wpf

.net-4.0

http://i.minus.com/i3xuoWZkpfxHn.png

I don't see anything that would let me pick files from my computer... there has to be one, where is it? I'm probably missing a reference?


Edit: What I had in mind was a textbox with a "Browse" button beside it. It occurs to me now that I probably have to place the textbox and browse button myself and add a click event to the button to open the dialog...

like image 638
mpen Avatar asked Oct 23 '11 02:10

mpen


People also ask

How do I select a file in WPF?

This article shows how to use an OpenFileDialog control in WPF and C# to browse files. Windows OpenFileDiloag dialog box lets users browse files on a computer. The dialog box not only lets you select a file but also allows you to set an initial directory, types of files to browse, and get selected file name.

What is the meaning of WPF?

WPF, stands for Windows Presentation Foundation is a development framework and a sub-system of . NET Framework. WPF is used to build Windows client applications that run on Windows operating system. WPF uses XAML as its frontend language and C# as its backend languages.


2 Answers

There is no built-in control that has a textbox with a [Browse] button beside it. You gotta set that up yourself.

For the "open file" dialog itself, there is the OpenFileDialog in Microsoft.Win32 namespace.

like image 103
Adam Lear Avatar answered Oct 09 '22 16:10

Adam Lear


For a more feature complete answer, assume you have a Button BtnFileOpen and a textbox TxtFile. First you need to reference the System.Windows.Forms assembly from the references dialog (make sure you check mark it, double clicking it didn't seem to add it for me).

Inside the button click event:

private void BtnFileOpen_Click(object sender, RoutedEventArgs e) {     var fileDialog = new System.Windows.Forms.OpenFileDialog();     var result = fileDialog.ShowDialog();     switch (result)     {         case System.Windows.Forms.DialogResult.OK:             var file = fileDialog.FileName;             TxtFile.Text = file;             TxtFile.ToolTip = file;             break;         case System.Windows.Forms.DialogResult.Cancel:         default:             TxtFile.Text = null;             TxtFile.ToolTip = null;             break;     } } 

If you have set your textbox to disabled you may wish to edit your xaml to include

ToolTipService.ShowOnDisabled="True"

like image 35
Chris Marisic Avatar answered Oct 09 '22 15:10

Chris Marisic