WPF 对话框不是模态的? [英] WPF Dialog not modal?

查看:80
本文介绍了WPF 对话框不是模态的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个窗口,我通过在 ui 线程上调度 ShowDialog() 调用来显示它(我在另一个线程上):

myMainWindowDispatcher.BeginInvoke(new Func(myDialog.ShowDialog));

myDialog 的所有者已设置为 myMainWindow.当对话框显示它总是正确的时候,但是我能够与后面的窗口进行交互!(违背了使其成为必需的模态的目的).我曾经做过同样的事情,即从另一个线程分派调用并且它曾经可以工作,即是模态的.现在对于我的生活,我无法弄清楚为什么不是.我无法在此处粘贴我的整个项目代码 - 你能想到什么会使 Window 非模态的吗??

解决方案

有趣:我挖了一个备份,找到了原因:

另一个对话框,使用 ShowDialog 显示的窗口,对话框 A,在这个对话框之前打开,然后这个对话框,对话框 B,显示在它上面.当对话框 B 加载时,我现在隐藏对话框 A,Window.Hide(),然后在对话框 B 关闭时再次显示它.对话框 A 的隐藏以某种方式使对话框 B 后面的其他窗口再次与用户交互,同时对话框 B 处于打开状态!

我猜测原因是因为一次显示多个对话框并不常见,当我隐藏其中一个时,WPF 认为它可以再次启用另一个 Windows..但这只是我的猜测!

一个解决方案是让你的另一个对话框变得非常小而不是隐藏(注意:将 Visibility 设置为 Hidden 与调用 Hide() 的结果相同):

 public void HideDialog(){myDialogA.SizeToContent = SizeToContent.Manual;myDialogA.Height = 0;myDialogA.Width = 0;}public void UnHideDialog(){myDialogA.SizeToContent = SizeToContent.WidthAndHeight;}

(显示多个对话框的业务需求超出了这个问题的范围,在我因为使用​​模态窗口而被称为邪恶 kanevil 之前,并且不相关,如果您想知道如何显示多个对话框,请参见此处:在 WPF 中显示多个对话框是否安全?)

I have a Window which I show by dispatching the ShowDialog() call on the ui thread (I am on another thread):

myMainWindowDispatcher.BeginInvoke(new Func<bool?>(myDialog.ShowDialog));

myDialog's Owner has been set to myMainWindow. When the dialog shows it is correctly always on top however I am able to interact with the Window behind! (defeating the purpose of making it modal which is required). I used to do the same thing, i.e. dispatch the call from another thread and it used to work, i.e. was modal. Now for the life of me I cannot figure out why it is not. I cannot paste my whole project code here - can you think of anything that would make the Window non modal??

解决方案

Interesting: I digged out a backup and found the cause:

Another dialog, Window shown using ShowDialog, Dialog A, is opened before this one, then this dialog, Dialog B, is shown ontop of it. When Dialog B has loaded I now hide Dialog A, Window.Hide(), then show it again when Dialog B closes. This hiding of Dialog A somehow makes other windows behind Dialog B interactive to the user again, while Dialog B is open!

I am guessing the reason is because showing multiple dialogs at once is not ordinary and when I hide one of them WPF thinks it can enable the other Windows again.. But that is just my guess!

A solution is to instead of hiding make very small your other Dialog (NOTE: setting Visibility to Hidden has the same result as calling Hide()):

    public void HideDialog()
    {
        myDialogA.SizeToContent = SizeToContent.Manual;
        myDialogA.Height = 0;
        myDialogA.Width = 0;
    }

    public void UnHideDialog()
    {
        myDialogA.SizeToContent = SizeToContent.WidthAndHeight;
    }

(The business requirement for showing multiple dialogs is beyond the scope of this question , before I get called evil kanevil for using modal windows, and not relevant, if you are wondering how one can show multiple dialogs see here: Is it safe to show multiple dialogs in WPF?)

这篇关于WPF 对话框不是模态的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆