window.Owner (behavior expected) - wpf

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.

Related

Wpf open all windows when click any of window in taskbar

In my wpf application, when user click on menu, a new window will open, and user click an item in that window, another window will open. And when these all window minimized the user, and click any of the window in taskbar, I need to open these three window.
When user click on any of the window, I want to open the three window.
WindowState is a property on Window.
Subscribe to StateChanged event on the window.
this.StateChanged += MainWindow_StateChanged;
When this changes to Maximized for one of the windows, update the state for other windows as well.
this.WindowState = System.Windows.WindowState.Maximized;

How can I iterate to the window control from a form

How can I iterate to the window control from a form in the below hiearchy?
Windows.Forms--> WPF UserControl --> WPF Window(Using ShowDialog).
LogicalTreeHelper.GetChildren does not catch the Window object.
A window that is opened by calling Show does not automatically have a relationship with the window that opened it; specifically, the opened window does not know which window opened it. This relationship can be established using the Owner property and managed using the OwnedWindows property. FRom here - Window.Show Method()
But you can't set Owner for your WPFWindow because 'Owner' property can get only WPFWindow but you have as owner WinForms window. Maybe you can just save WindowControl as instance variable in you class so then you can get it any time you want.

How To Make WPF ShowDialog() Block Parent Window

WPF 4.5
I've got an app where I show a modal Window from my app's main Window by instantiating it, setting its owner to be the main Window, and then calling ShowDialog(). The modal Window has "Topmost=true". The main Window does not.
When I run my app the modal Window shows in front of the main Window, and it is kept in front of the main Window just as expected. However, I can simply click on the main Window behind the modal Window to Activate the main Window and manipulate it...I can even close it!
In my humble opinion this is definitely not the desired behavior for an application with a modal Window. I'm confused as to why WPF handles it this way. More important, I need a solution that will keep the modal Window in front while also blocking access to the main Window behind it (isn't that supposed to be a basic function of a modal Window?)
I believe this desired behavior has always been the default behavior when using ShowDialog in WinForms (way back in the day.) What am I missing here, and how can I get this working with WPF?
You have to set the owner window before ShowDialog :
modalWindow.Owner = RootWindow;

Who owns the process / main thread?

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

Closing a Window in WPF (When not in that Window)

I have an application which starts with a simple start screen allowing the user to select either New or Open a project. When selecting New I have a new window displayed which is a wizard that collects data to be passed to the Main window.
I create a new Window for the Main window and show that.
Then I close the wizard easily enough with this.close();
But how do I close the initial window which is the Startup URI window?
Application.Current.MainWindow.Close();
Pass the Startup window to the main window as a constructor parameter or property, then call Close() on it.

Resources