Who owns the process / main thread? - wpf

I created new WPF Application witn two windows.
MainWindow.xaml
Window1.xaml
Added one button in MainWindow.xaml and wrote the following code in the click event of the button:
Window1 w = new Window1();
w.Show();
I clicked that button 2-3 times and it opened multiple instances of Window1.
Everything fine till here.
Now, I closed MainWindow and it did not close the instances of Windows1.
I was thinking that since MainWindow is the one that owns the Process and if it closes, rest of the child windows will close automatically.
I did the same project in WinForms application and the result was completely different. On closing the MainWindow, it did close rest of the windows.
Am i not understanding the concept clearly? Can someone tell me who owns the process or the main thread in WPF?

The other windows are not child windows of your MainWindow unless you set window.Owner = mainWindow;
Further there is the Application.MainWindow property which sets which window actually is treated as "the" main window. This affects the Application.ShutdownMode if set to OnMainWindowClose.
Excerpt from the Window.Owner reference:
When a child window is opened by a parent window by calling ShowDialog, an implicit
relationship is established between both parent and child window. This relationship enforces certain behaviors, including with respect to minimizing, maximizing, and restoring.
When a child window is created by a
parent window by calling Show,
however, the child window does not
have a relationship with the parent
window. This means that:
The child window does not have a reference to the parent window.
The behavior of the child window is not dependent on the behavior of
the parent window; either window can
cover the other, or be minimized,
maximized, and restored independently
of the other.
To allow you to create a relationship
between a child window and a parent
window, Window supports the notion of
ownership. Ownership is established
when the Owner property of a window
(the owned window) is set with a
reference to another window (the owner
window).
The application root class: System.Windows.Application
WPF Threading Model

Related

Constraining child windows

I'm writing a WPF application to control an industrial process. The main window class spawns child windows, each to control a separate piece of equipment. These child windows can be dragged but not resized.
The child windows are owned by the parent so they stay on top, but the problem this creates is that they can be dragged on top of the main window menu, which is considered aesthetically unacceptable by my client.
It would be OK if the child window goes under the menu or if it goes right up to the menu and stops but it's not OK to obscure the main menu. How can I achieve this? Can it be done within WPF without going into Win32 and unmanaged code?
Thanks in advance.
You could try to set Topmost=True on the parent window, and make it only contain the main menu. So, you'd basically have one window for the main menu and a couple of others for everything else. Or, if you can't do that, you can create a main menu window with Topmost=True as a child window of your parent, and move both of them together whenever one of them changes position.

Child wpf window above parent window

I have WPF application with main window. I want to create child WPF window which always must be above ONLY parent window. If I set TopMost property for new window then window is above ALL nonTopMost windows on desktop. It's not what I want.
Set the Owner property of the child window so that it refers to the parent window.
child.Owner = parent;
Depending on the nature of the window, I often use a "Fake" window that is really just a layer in the parent along with a partailly transparent grey layer between that makes the parent look ghosted while the child is active. You can then keep the child window set to collapsed until it is needed.

pass message from the parent window to child user control wpf

I have a window that is hosting a user control. I wish to pass (raise) events in the parent window that should reach the user control.
How do I define events in the parent window that the child user control can also receive.
As I recall the steps should be:
Define a delegate in your parent window
Define an event in your parent window
Lets say in your window Loaded event attach the usercontrol to the
event, eg -
this.MyEvent += new
this.MyDelegate(this.UserControl1.SomeMethod);
Take note that
SomeMethod must match the MyDelegate definition.
Raise the event in your parent window per application logic.
For examples of how to define delegates/events then google is your friend ;) Also don't forget to unregister your usercontrol from the event, when it's no longer required (when the window closes).

window.Owner (behavior expected)

what am I missing having this code behind on a XAML window
// Create a window and make this window its owner
Window ownedWindow = new Window();
ownedWindow.Owner = this;
ownedWindow.ShowDialog();
I don't see the behavior
http://msdn.microsoft.com/en-us/library/system.windows.window.owner.aspx
Once this relationship is established, the following behaviors are exhibited:
•If an owner window is minimized, all its owned windows are minimized as well.
•If an owned window is minimized, its owner is not minimized.
•If an owner window is maximized, both the owner window and its owned windows are restored.
•An owner window can never cover an owned window.
•Owned windows that were not opened using ShowDialog are not modal. The user can still interact with the owner window.
•If you close an owner window, its owned windows are also closed.
•If an owned window was opened by its owner window using Show, and the owner window is closed, the owned window's Closing event is not raised.
TIA
The ShowDialog() method shows the window as a modal dialog, overriding most of this behavior.
You should call Show() instead to show the window as a child window.
Note that Show() is not a blocking call; your next line of code will execute immediately, not after the window is closed.

How to stop WPF TopMost flag on parent window from being inherited by the child

I have a WPF window that has TopMost=true . When I call another Window from this window and specify the topmost window as the parent the owned window also displays as TopMost.
I would like to find a way to stop that from happening so that my parent can still own the child yet the child does not have TopMost=True.
I know I can just not bother to set the owner on the child then I won't get the TopMost flag, but I need all my windows to close with the parent window and writing the logic to handle that seems like a waste when it is included.
I've tried to explicitly set the TopMost=False after the child was loaded but no luck, it doesn't seem to matter if the owner window is TopMost then the child will be no matter what I do to it's TopMost property.
Any ideas?
Set The TopMost of the (parent) window, at runtime. (and not by default)

Resources