Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting DialogResult only after ShowDialog() in WPF

Tags:

dialog

wpf

I have a window that I sometimes open using Show() and sometimes using ShowDialog(). In the second case, the returned dialog result is important for me. But if I set the DialogResult after calling Show() I get an InvalidOperationException. Is there a way to find out which method was used to open the window and set or not the DialogResult accordingly? Or is there another way?

Of course I know I can catch and ignore the exception, but I don't like this solution.

like image 760
svick Avatar asked Sep 04 '09 11:09

svick


2 Answers

Use System.Windows.Interop.ComponentDispatcher.IsThreadModal inside the window to determine if it runs on a modal thread or not.

like image 65
Pop Catalin Avatar answered Sep 21 '22 18:09

Pop Catalin


If you look at set_DialogResult in Reflector, it checks _showingAsDialog to determine whether the dialog is modal. Unfortunately this is a private field.

Do you always construct a new instance of the window before calling Show()/ShowDialog(). If so, you could pass an argument to the constructor indicating how it is to be shown.

like image 24
Phil Devaney Avatar answered Sep 22 '22 18:09

Phil Devaney