Click here to Skip to main content
15,891,981 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
In WPF Application one window call ShowDialog for other and other. And I see all window chain. When I hide the calling window before I show other - ShowDialog returns when Visibility = System.Windows.Visibility.Visible. For all windows I added this.Closed += Window_Closed; for show previous window
C#
void Window_Closed(object sender, EventArgs e)
{
    if (Owner != null)
    {
            Owner.Visibility = System.Windows.Visibility.Visible;
    }
}

and before call ShowDialog I hide the active window
{
this.Visibility = System.Windows.Visibility.Collapsed;
CustomerData maCreateCD = new CustomerData();
maCreateCD.Owner = this;
maCreateCD.ShowDialog();
}
This operation repeated for other windows and all OK. I see only last window. But when I call Close() in last window I see that maCreateCD.ShowDialog(); is returned and for other windows ShowDialog is returned. And only after this the Visibility property for window called before last is set to Visible and window shows.
In MSDN ShowDialog is returned only after window closing but this does not work. I see that ShowDialog is returned and window does not closed.
Posted
Comments
Philippe Mori 11-Nov-14 20:55pm    
Usually, you don't hide modal Windows nor owner Windows.
edtrojan 12-Nov-14 1:44am    
In WPF for windows chain (ShowDialog -> ShowDialog -> ShowDialog -> ShowDialog) I can see all modal windows but I want to see only last dialog.

1 solution

Instead of showing next dialog from the previous one, a much better alternative would be to close the dialog first and then show next dialog. Something like :
C#
var result = dialog1.ShowDialog();
if (some_conditions)
{
  var result2 = dialog2.ShowDialog();
}
if (some_other_conditions)
{
  var result3 = dialog3.ShowDialog();
}
if (some_final_conditions)
{
  var result4 = dialog4.ShowDialog();
}

I would say that each dialog would use the main window as their owner. Thus doing it that way, it might be possible to first close the old dialog and show the new one from the same event handler.

I can say that modal dialog are not designed to be hidden. This scenario is af far as I understand not supported and thus will not behave well. A modal dialog is designed to be shown until its end and after that the code continue at the caller location just after the call of ShowDialog.

Many things ca goes wrong if you try to make a workaround. In particular, enabling, flickering, taskbar buttons, focus might not bahave correctly and thus the application won't work well with the system. Avoid hiding modal Windows.

Alternatively, if it is a wizard, then you should simply show a user control for each page Inside the same dialog.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900