views:

9494

answers:

3

I am writing my first application in WPF and want to have the user input some data on a modal dialog window. Apparently, this is not simple to do in WPF, because the parent window stays fully enabled, and the method that created the new child window doesn't stop and wait for the child window to call Close(). Instead it just keeps going forward. This is not what I want.

How can I get the child window to open, and have the parent window wait for the child to close before the parent window continues executing?

+15  A: 

Did you try showing your window using the ShowDialog method?

Yordan Pavlov
I'm learning, hehe... Very nice, thank you
Alex Baranosky
Don't forget to set the Owner property on the dialog window. Otherwise, the user will get weird behavior when Alt+Tabbing, etc.
Edward Brey
+2  A: 

Given a Window object myWindow, myWindow.Show() will open it modelessly and myWindow.ShowDialog() will open it modally. However, even the latter doesn't block, from what I remember.

harms
I believe it blocks. Code after the myWindow.Show() is not executing until after myWindow calls Close().
Alex Baranosky
+3  A: 

Window.Show Window will show the window, and continue execution -- it's a non-blocking call.

Window.ShowDialog will block the calling thread (kinda [1]), and show the dialog. It will also block interaction with the parent/owning window. When the dialog is dismissed (forwhatever reason) ShowDialog will return to the caller, and will allow you to access DialogResult (if you want it).

[1] It will keep the dispatcher pumping by pushing a dispatcher frame onto the WPF dipatcher. This will cause the message pump to keep pumping.

dhopton
explain this in more detail please? I'm looking at a similar problem where I have a test process running but warning messages can pop up as modal dialogs but i don't want to block execution.
Firoso