简体   繁体   English

WPF中的窗口所有者没有始终在线的行为

[英]Window owner in WPF without always-on-top behaviour

Is it possible to get some of the functionality of Window.Owner without getting all of it? 是否有可能获得Window.Owner一些功能而无法获得所有功能?

There are two windows, window A and window B. I want to make it so that selecting either one will bring them on top of other applications, but either one can overlay the other. 有两个窗口,窗口A和窗口B.我想这样做,以便选择任何一个将使它们在其他应用程序之上,但任何一个可以覆盖另一个。 (In reality there more than just two, but they should all behave similarly.) (实际上不仅仅是两个,但它们的行为应该相似。)

If I set window B's Owner to A, then switching to either window will bring both in front of other applications (which I want), but will also force B to always sit on top of A (which I don't want). 如果我将窗口B的Owner设置为A,那么切换到任一窗口都会将两者都放在其他应用程序(我想要的)之前,但也会强制B始终位于A的顶部(我不想要)。

I actually already have code which is tracking the window hierarchy independently of Owner / OwnedWindows , so I can probably extend that to sort out the activation problem. 实际上我已经有了独立于Owner / OwnedWindows跟踪窗口层次结构的代码,所以我可以扩展它以解决激活问题。 So if that simplifies the problem, an alternative answer I'm looking for is: 因此,如果这简化了问题,我正在寻找的另一个答案是:

How do I actually do "when this window is activated by the user, bring a specific set of windows (all the others in the app) to the Z-order just below me, while preserving their existing Z-orders relative to each other"? 我如何实际做“当用户激活此窗口时,将一组特定窗口(应用程序中的所有其他窗口)带到我下面的Z顺序,同时保留它们相对于彼此的现有Z顺序” ?

One possible solution would be to have a hidden window that owns all the windows in your app. 一种可能的解决方案是拥有一个拥有应用程序中所有窗口的隐藏窗口。

You would declare it something like: 你会声明它像:

<Window
    Opacity="0"
    ShowInTaskbar="False"
    AllowsTransparency="true"
    WindowStyle="None">

Be sure to remove StartupUri from your App.xaml. 请务必从App.xaml中删除StartupUri。 And in your App.xaml.cs you would override OnStartup to look something like: 在您的App.xaml.cs中,您将覆盖OnStartup,如下所示:

protected override void OnStartup(StartupEventArgs e)
{
    HiddenMainWindow window = new HiddenMainWindow();
    window.Show();

    Window1 one = new Window1();
    one.Owner = window;
    one.Show();

    Window2 two = new Window2();
    two.Owner = window;
    two.Show();
}

Another difficulty will be how you want to handle closing the actual application. 另一个困难是你想要如何处理关闭实际的应用程序。 If one of these windows is considered the MainWindow you can just change the application ShutdownMode to ShutdownMode.OnMainWindowClose and then set the MainWindow property to either of those windows. 如果其中一个窗口被视为MainWindow,您只需将应用程序ShutdownMode更改为ShutdownMode.OnMainWindowClose,然后将MainWindow属性设置为其中一个窗口。 Otherwise you will need to determine when all windows are closed and call Shutdown explicitly. 否则,您需要确定所有窗口何时关闭并明确调用Shutdown。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM