Reputation: 21
I have a WPF application which mostly works just fine. However a couple of people have reported that when they press the browse button nothing happens. The code for this seems pretty simple:
private void btnBrowseFile_Click(object sender, RoutedEventArgs e)
{
Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();
dlg.Filter = "Supported Types (*.xml)|*.xml";
Nullable<bool> result = dlg.ShowDialog();
if (result == true)
{
Environment is Windows 7, .Net 3.5 SP1, single monitor. Apparently no exception occurs and the application isn't hung. The OpenFileDialog simply fails to appear.
Any ideas?
Thanks,
Barrie
Upvotes: 2
Views: 2098
Reputation: 36775
Not really a solution but an idea:
Try to use the ShowDialog(Window)
-signature. I have never had a problem as you described and I always use the mentioned signature. Maybe there is a problem automatically detecting the owner.
Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();
dlg.Filter = "Supported Types (*.xml)|*.xml";
Nullable<bool> result = dlg.ShowDialog(Window.GetWindow(this));
if (result == true)
{
Another option would be to use the Win-Forms dialog. However this has the disadvantage of loading the corresponding assembly. But if you don't open the dialog at app startup, I think this should not be a big problem.
I would also look what can prevent firing the event handler. IMO it's more likely that the event handler never will be called than that a problem with the OpenFileDialog exists.
Upvotes: 1