简体   繁体   中英

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?

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. (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).

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

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. And in your App.xaml.cs you would override OnStartup to look something like:

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. Otherwise you will need to determine when all windows are closed and call Shutdown explicitly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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