WPF ShowDialog returns immediately

Posted by dthrasher on Stack Overflow See other posts from Stack Overflow or by dthrasher
Published on 2010-05-11T15:30:04Z Indexed on 2010/05/11 15:34 UTC
Read the original article Hit count: 1054

Filed under:
|

Sometimes when I call ShowDialog on an OpenFileDialog Window in WPF, the dialog closes immediately with a return value of false.

I'm calling ShowDialog in response to a button click event. I can reproduce this problem using the sample code for OpenFileDialog on MSDN:

// Configure open file dialog box
Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();
dlg.FileName = "Document"; // Default file name
dlg.DefaultExt = ".txt"; // Default file extension
dlg.Filter = "Text documents (.txt)|*.txt"; // Filter files by extension

// Show open file dialog box
Nullable<bool> result = dlg.ShowDialog();

// Process open file dialog box results
if (result == true)
{
    // Open document
    string filename = dlg.FileName;
}

The problem occurs intermittently when I run my solution in Debug mode from Visual Studio 2008 SP1. It's quite annoying.

Is this a known issue? Are there workarounds?

© Stack Overflow or respective owner

Related posts about wpf

Related posts about visual-studio-2008