Where's the file-picker dialog in WPF?

C#Wpf.Net 4.0

C# Problem Overview


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...

C# Solutions


Solution 1 - C#

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.

Solution 2 - C#

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"

Solution 3 - C#

I generally just use the OpenFileDialog in the System.Windows.Forms namespace. I alias it using SWF, and then it just becomes

SWF.OpenFileDialog o = new SWF.OpenFileDialog();

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionmpenView Question on Stackoverflow
Solution 1 - C#Adam LearView Answer on Stackoverflow
Solution 2 - C#Chris MarisicView Answer on Stackoverflow
Solution 3 - C#joeView Answer on Stackoverflow