WPF: How to Hide a modal dialog box without destroying it? (its DialogResult)

2.5k views Asked by At

I've got a modal dialog box and (when user presses its OK button) I want to hide it, show another modal dialog box (MessageBox for example) and then show it back again. My problem is that when the dialog is hidden, its DialogResult gets false and of course its getting closed right after the button's handler method ends. I've even tried to set Opacity to 0 instead of Hide() but that doesn't work at all (it's still visible).

Is there a way to hide a modal dialog box for a moment without closing it?

3

There are 3 answers

1
paparazzo On

This does not deal with the result but see how to return data from a Page
PageModal is a Page
You use NavigationWindow for the modal part

public partial class MainWindow : Window
{
    private PageModal pageModal = new PageModal();
    public MainWindow()
    {
        InitializeComponent();
    }

    private void btnLaunchModal(object sender, RoutedEventArgs e)
    {
        NavigationWindow navWindow = new NavigationWindow();
        navWindow.Content = pageModal;
        navWindow.ShowDialog();
    }
}
0
P.W. On

Ok, the opacity IS working. I just had it blocked by finished animation (with HoldEnd behavior) and I didn't knew about it. So, if anyone has the same problem and needs to hide a modal window, the Opacity = 0; is the solution.

0
Wolf5 On

Workaround that is working for me:

To prevent the window from being closed once you set the DialogResult, create your own DialogResult instead:

public new bool DialogResult;

Now you can still set the variable and choose Hide() instead of Close(). So all the places where DialogResult is set I add a

Hide();

So i looks like this:

DialogResult=true;
Hide(); 

or

DialogResult=false;
Hide(); 

That way I can do a new ShowDialog() again.

So if I need to reopen the window until the content is correct (if validation happens after closing), it would look something like this:

    public void ShowDialog()
    {
        var dialog = new MyDialog();
        bool ok = false;
        while (!ok)
        {
            dialog.ShowDialog();
            if (dialog.DialogResult)
            {
                ok = DoSomeValidation();
            }
            else
            {
                ok = true;
            }
        }
    }