当应用程序聚焦时始终显示“模态"对话框 [英] Show Modal dialog always when the application is focused

查看:103
本文介绍了当应用程序聚焦时始终显示“模态"对话框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

单击主窗体中的按钮后,会显示一个模式对话框

使用ShowDialog方法,我可以在主窗体上方显示子模式对话框.
但是,如果我在应用程序外部单击,则假设我选择了其他应用程序,然后返回到我的应用程序.
当我的应用程序被选中时,我应如何使子模式"对话框成为焦点?
在此先感谢

I have a modal dialog shown on click of a button in the main form

using the ShowDialog method i am able to show the child modal dialog on top of my main form.
But if i click outside my application suppose if i select some other application and come back to my application .. the main form is focused..

How shall i make my Child modal dialog focused when my application is seleced ??
Thanks in advance

推荐答案

有两个Window属性可控制应用程序激活的行为.如果正确使用它们,则该应用程序的所有窗口都将像一个应用程序一样工作,因此任何其他应用程序的窗口都不会以Z顺序出现在它们之间. 对于所有窗口,不仅是主窗口和模态窗口,都将实现此行为.

第一个是System.Windows.Window.Owner:
http://msdn.microsoft.com/en-us/library/system. windows.window.owner.aspx [ ^ ].

为了实现正确的行为,您需要(自然)不保留主窗口,而所有其他窗口应归主窗口所有.更为复杂的所有权关系是可能的,但意义不大.

第二个并不是那么重要,它只是增加了一些一致性:对于主窗口,应将布尔属性ShowInTaskbar设置为true,对于所有其他窗口设置为false.请参阅:
http://msdn.microsoft.com/en-us/library/system. windows.window.showintaskbar.aspx [ ^ ].

现在,让我们看一下ShowDialog上的文档:
http://msdn.microsoft.com/en-us/library/system. windows.window.showdialog.aspx [ ^ ].

There are two Window properties which control the behavior of the activation of the application. If you use them properly, all the windows of the application will behave like one application, so no window of any other application cannot appear in between in Z order. This behavior will be achieved for all windows, not only the main and the modal ones.

First one is System.Windows.Window.Owner:
http://msdn.microsoft.com/en-us/library/system.windows.window.owner.aspx[^].

To achieve right behavior, you need to leave the main window not owned (naturally), and all other windows should be owned by the main one. More complex ownership relation is quite possible but makes not much sense.

The second one is not so important, it only adds some consistency: you should set the Boolean property ShowInTaskbar to true for the main window, false for all others. Please see:
http://msdn.microsoft.com/en-us/library/system.windows.window.showintaskbar.aspx[^].

Now, let''s see at the documentation on ShowDialog:
http://msdn.microsoft.com/en-us/library/system.windows.window.showdialog.aspx[^].

MSDN 说:

通过调用ShowDialog方法打开的窗口不会自动与打开它的窗口有关系.具体来说,打开的窗口不知道哪个窗口打开了它.可以使用Owner属性建立此关系,并使用OwnedWindows属性进行管理.

A window that is opened by calling the ShowDialog method does not automatically have a relationship with the window that opened it; specifically, the opened window does not know which window opened it. This relationship can be established using the Owner property and managed using the OwnedWindows property.



因此,您还需要设置模式窗口的属性Owner,以获得预期的行为.请试试.我不明白为什么不能自动通过ShowDialog完成此操作,但事实是:WPF应用程序未提供了解主窗口的默认方式(您可以自己开发此类代码),以及方法没有用于指示所有权的参数.

请尝试一下.如果它不能解决您的问题,那就意味着您在其他地方搞砸了,但是我不这么认为.如果有帮助,请报告.

—SA



So, you need to set the property Owner of the modal window as well, to get expected behavior. Please try. I don''t understand why this is not done through ShowDialog, automatically, but the fact is: WPF application does not provide a default way of knowing the main window (you can develop such code yourself), and the method ShowDialog does not have a parameter to indicate ownership.

Please try it. If it does not resolve your problem, it would mean you screw up something somewhere else, but I don''t thinks so. Please report back if it helps or not.

—SA


这篇关于当应用程序聚焦时始终显示“模态"对话框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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