Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF owner window on top of child window

Is it possible for Owner window in WPF be on top of Child window when you click on it while Owner window is below Child window?

here is example how I call child window:

Window2 window = new Window2();
window.Owner = this;
window.Show();

Parent/Owner window will always be under child window.

like image 761
Andrija Avatar asked Jun 29 '09 08:06

Andrija


3 Answers

I ran into a similar situation. I solved this by simply removing the owner after showing the window.

Window2 window = new Window2();
window.Owner = this;
window.Show();
window.Owner = null;

Edit: Someone replied to this, and while looking at it, I decided I wanted to make an extension method.

public static void ShowAsIfChildOf(this Window childWindow, Window parentWindow)
{
  childWindow.Owner = parentWindow;
  childWindow.Show();
  childWindow.Owner = null;
}
like image 152
Licht Avatar answered Nov 18 '22 18:11

Licht


Many of the answers on this page involve nulling-out the Window.Owner property for some or all of the (non-MainWindow) windows in your System.Windows.Application. While this is a simple and easy fix that does indeed, in isolation, fix the issue of Window overlap, unfortunately it also inhibits a lot of useful application-wide functionality that WPF seems otherwise eager to provide in the areas of:

  • Application acti­vation/deactivation (via mouse-click, desktop Alt-Tab switching, etc...),
  • correct observance of the Application.​ShutdownMode property, and generally,
  • orderly cleanup, resource disposal, and exit of your Application upon shutdown.

It is possible fix the Window overlap issue while still preserving these system-wide WPF features by instead designating a special invisible window instance as your Application.MainWindow.

Modify your application so that the first Window it creates--which is the Window that gets assigned to Application.MainWindow--is a special dummy Window that is then made invisible by setting its Visibility to Visibility.Hidden or calling Window.Hide(). This will not hide its owned windows. Then, ensure that your desired "main" window containing your true content, plus all the other windows, owned by this invisible window.

Once hidden, the dummy Window will not show in the Windows 10 taskbar. You can set the Window.ShowInTaskbar property on whichever of the visible windows you deem appropriate to simulate apparent special designation, as required.

This approach allows any of the visible windows in your Application to be on top of the others, while still preserving WPF features and behaviors for system-wide app activation. For example, when the Application is activated (by clicking on any one of the windows, or via Alt-tab), all of the application's windows are together brought above any other desktop app windows, while still preserving the most recent "in-app" Z-order. WPF shutdown functionality is also preserved, including correct observation of the Application.ShutdownMode logic in accordance with the invisible MainWindow (or all the others) being closed.

like image 23
Glenn Slayden Avatar answered Nov 18 '22 20:11

Glenn Slayden


To get the behavior you want, you don't want to set the Owner on either window.

You, of course will have to handle the logic yourself when closing either of the windows to close your imaginary "child" window.

There may be some other logic you'll have to implement related to minimizing, maximizing, etc.

like image 7
jonathanpeppers Avatar answered Nov 18 '22 19:11

jonathanpeppers