Note that there are some explanatory texts on larger screens.

plurals
  1. POWPF App loses completely focus on window close
    primarykey
    data
    text
    <p><strong>Problem description</strong></p> <p>If I make a non-modal window as a child window through setting the Owner of the window to a parent window, and then show a MessageBox from within this child window, the parent window will lose focus if I close the child window. If windows explorer or another app is open, this app will get the focus and my main window will be hidden.</p> <p>This seems to be a known problem as I saw it in another <a href="http://social.msdn.microsoft.com/forums/en-US/wpf/thread/a51ac7b9-7021-4669-accd-af0e86b9cc01" rel="noreferrer">newsgroups</a>, but I don’t have seen a good solution. Setting the owner to null in OnDeactivate is not an option. Setting the owner before showing the MessageBox to null and resetting after that doesn’t help. Setting the owner to null in the OnClosed event does also not help.</p> <p><strong>Simple Solution found</strong></p> <p>If you experience the same problem as I have described, put the following code in the OnClosing of all child windows.</p> <pre><code>void OnClosing(System.ComponentModel.CancelEventArgs e) base.OnClosing(e); if (null != Owner) { Owner.Activate(); } // .... </code></pre> <p>It can be followed by any further processing logic, even opening MessageBoxes is tolerated.</p> <p><strong>Example-Code</strong></p> <p>The issue seems to be much bigger as I thought. The following example will remove focus of the parent window if the message box will be opened and the the child window will be closed (Copy the code into a loaded event-handler of a Window).</p> <pre><code>Window firstChildWindow = new Window() { Title = "Floating Window", Width = 100, Height = 70 }; firstChildWindow.Owner = Window.GetWindow(this); Button button = new Button() { Content="MessageBox"}; button.Click += delegate { MessageBox.Show("Klicking her breaks the focus-chain."); }; firstChildWindow.Content = button; firstChildWindow.Show(); </code></pre> <p>Also this example breaks the focus-chain:</p> <pre><code>Window firstChildWindow = new Window() { Title = "Floating Window", Width = 100, Height = 70 }; firstChildWindow.Owner = Window.GetWindow(this); firstChildWindow.Show(); Window secondChildWindow = new Window() { Title="Second Window",Width=100,Height=70}; secondChildWindow.Content = new TextBlock() { Text="SecondWindow"}; secondChildWindow.Owner = firstChildWindow; secondChildWindow.Show(); </code></pre> <p>Has someone a resolution for this problem. I think about a hack to trigger giving focus to the parent after closing, with Dispatcher or DispachterTimer or perhaps it would be work to manualy force focus to the parent on closed but this all seems to me very unclean (and is also a little complicated if there are more active owned windows of the same parent, as I have in my current app). </p> <p>No one knows a neat solution to this?</p> <p><strong>Resources</strong></p> <p><a href="http://msdn.microsoft.com/en-us/library/system.windows.window.owner.aspx" rel="noreferrer">MSDN Description</a> (see under remarks for non modal windows opened calling Show())</p> <p><a href="http://social.msdn.microsoft.com/forums/en-US/wpf/thread/a51ac7b9-7021-4669-accd-af0e86b9cc01" rel="noreferrer">Same problem on msdn forums without appropriate solution</a></p> <p>Please see also: <a href="https://stackoverflow.com/questions/3234581/instable-focus-of-wpf-apps">Instable focus of WPF apps</a></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload