如何隐藏一个模态对话框而不破坏它?(它的DialogResult)

时间:2021-07-14 11:09:00

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).

我有一个模态对话框(当用户按下OK按钮时)我想要隐藏它,显示另一个模态对话框(例如MessageBox),然后再次显示它。我的问题是,当对话框被隐藏时,它的对话框会出错,当然在按钮的处理程序结束后它会被关闭。我甚至尝试过将不透明度设置为0而不是Hide(),但这根本不起作用(它仍然是可见的)。

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

是否有一种方法可以隐藏一个模态对话框而不关闭它?

3 个解决方案

#1


0  

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

这并不处理结果,但是可以看到如何从页面PageModal返回数据,PageModal是模式部分使用NavigationWindow的页面

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();
    }
}

#2


0  

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.

不透明度在起作用。我刚被完成的动画(使用HoldEnd行为)屏蔽了,我不知道。如果有人有同样的问题需要隐藏一个模态窗口,不透明度= 0;是解决方案。

#3


0  

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;

公共新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()而不是Close()。所以所有设置对话框的地方都加a

Hide();

隐藏();

So i looks like this:

我看起来是这样的:

DialogResult=true;
Hide(); 

or

DialogResult=false;
Hide(); 

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

这样我就可以再次执行一个新的ShowDialog()。

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;
            }
        }
    }

#1


0  

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

这并不处理结果,但是可以看到如何从页面PageModal返回数据,PageModal是模式部分使用NavigationWindow的页面

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();
    }
}

#2


0  

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.

不透明度在起作用。我刚被完成的动画(使用HoldEnd行为)屏蔽了,我不知道。如果有人有同样的问题需要隐藏一个模态窗口,不透明度= 0;是解决方案。

#3


0  

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;

公共新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()而不是Close()。所以所有设置对话框的地方都加a

Hide();

隐藏();

So i looks like this:

我看起来是这样的:

DialogResult=true;
Hide(); 

or

DialogResult=false;
Hide(); 

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

这样我就可以再次执行一个新的ShowDialog()。

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;
            }
        }
    }