开发者

WPF Dialog not modal?

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 how开发者_StackOverflowever 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?)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜