WPF Dialog not modal?

和自甴很熟 提交于 2019-12-11 04:28:06

问题


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??


回答1:


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



来源:https://stackoverflow.com/questions/6420296/wpf-dialog-not-modal

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!