Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8783055
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T20:43:51+00:00 2026-06-13T20:43:51+00:00

I have noticed this very odd behavior in a WPF application. I have a

  • 0

I have noticed this very odd behavior in a WPF application.

I have a MainWindow, which is shown using Show() from App.OnStartup. Said MainWindow can open a (non-modal) SubWindow, also using Show(). SubWindow‘s Owner is set to MainWindow.

When SubWindow is closed, MainWindow is visible again (good).

Some actions can cause the SubWindow to open a third window as a modal dialog, using ShowDialog() (Owner is set to SubWindow). When that modal dialog is opened and closed at least once during the lifetime of a SubWindow, then the weird thing happens.

After closing SubWindow, MainWindow does not come into view. Instead, whatever random window is behind MainWindow comes into view. Can anyone explain to me why this happens, and how to fix it?

It makes no difference whether the modal dialog is a normal Window displayed using ShowDialog(), or a message box shown using MessageBox.Show().


Here is some minimal code to reproduce this. Create a new WPF application in visual studio, and paste this into the pre-generated MainWindow.xaml.cs

Then, press a key on the keyboard to open only one window, close it, behavior as expected. Press two keys, close both, then the very first window is behind Visual Studio (presumably).

public MainWindow()
{
    InitializeComponent();
    this.PreviewKeyDown += (sender, e) =>
    {
        if (this.Owner is MainWindow)
        {
            // we're the SubWindow

            MessageBox.Show("I am a modal dialog");

            // code below produces the exact same behavior as the message box

            //var dialog = new MainWindow();
            //dialog.Owner = this;
            //dialog.ShowDialog();
        }
        else
        {
            // we're the initial MainWindow created by App.
            var subWindow = new MainWindow();
            subWindow.Owner = this;
            subWindow.Show();
        }
    };
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T20:43:53+00:00Added an answer on June 13, 2026 at 8:43 pm

    This is a pretty annoying WPF bug, I never did find the flaw in the code that causes it but there’s a heckofalot of “gotta figure this out” comments in the source code that deals with focusing. Just a workaround, a less than ideal one, you can solve it by explicitly giving the focus to the owner when the window is closing. Copy/paste this code in your SubWindow class;

        protected override void OnClosing(System.ComponentModel.CancelEventArgs e) {
            base.OnClosing(e);
            if (!e.Cancel && this.Owner != null) this.Owner.Focus();
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a multithreaded application. However I noticed, I'm using OpenGL and this should
I have noticed that on the iPad the google maps app has a very
I noticed something odd today. I have an application that writes/read data to a
I was reading this very useful article and noticed that a Windows Phone application
I have noticed a very odd behaviour with an IF-statement in Progress 4gl. I
I have noticed that during very processor or memory intensive processes that my app
I have noticed a number of kernel sources that look like this (found randomly
I'm new to jQuery and have noticed someone go: var $this = $(this); Why
This question may not directly relate to programming. I have noticed that the technology
I noticed that some stylesheets have something like this: body { font-size: 62.5%/1.2em; }

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.