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?
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.
Did you try showing your window using the ShowDialog method?
Don't forget to set the Owner property on the dialog window to the main window. This will avoid weird behavior when Alt+Tabbing, etc.
A lot of these answers are simplistic, and if someone is beginning WPF, they may not know all of the "ins-and-outs", as it is more complicated than just telling someone "Use
.ShowDialog()
!". But that is the method (not.Show()
) that you want to use in order to block use of the underlying window and to keep the code from continuing until the modal window is closed.First, you need 2 WPF windows. (One will be calling the other.)
From the first window, let's say that was called MainWindow.xaml, in its code-behind will be:
Then add your button to your XAML:
And right-click the
Click
routine, select "Go to definition". It will create it for you in MainWindow.xaml.cs:Within that function, you have to specify the other page using its page class. Say you named that other page "ModalWindow", so that becomes its page class and is how you would instantiate (call) it:
Say you have a value you need set on your modal dialog. Create a textbox and a button in the
ModalWindow
XAML:Then create an event handler (another
Click
event) again and use it to save the textbox value to a public static variable onModalWindow
and callthis.Close()
.Then, after your
.ShowDialog()
statement, you can grab that value and use it: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.