13

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

Miral
  • 12,637
  • 4
  • 53
  • 93
  • 1
    +1 I have the same requirement; did you find a solution already? For your z-order question: you can do this by keeping a stack (just a List) and registering for the Activated event of all your windows. On each event, you remove the winodw from the stack and put it back on top (== list.Add). The foreach window in the list brind it to the foreground (using interop). – stijn Dec 30 '11 at 12:19
  • I haven't found another solution yet. Todd's answer below seems promising and is probably what I'll use (unless something better comes up), but I haven't managed to test it yet as the project that this was for has been back-burnered. (Which is why I haven't ticked the answer yet. I promise, I will get back to it eventually!) – Miral Jan 18 '12 at 04:29

1 Answers1

5

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.

Todd White
  • 7,870
  • 2
  • 35
  • 34
  • this is actually almost perfect. Handling closing/minimizeing/restore is not too difficult, as I'm using an event aggregator I just publish events upon creating/changing windows and keep a central list of all of them. The sole withdrawal I found is that upon alt-tab windows will show an empty entry (except for an icon) because of the transparent owner window. – stijn Dec 30 '11 at 12:21