Question

The following code demonstrates an issue I'm having where closing a child window minimizes the parent window, which I dont want to happen.

    class SomeDialog : Window
    {
        protected override void OnMouseDoubleClick(MouseButtonEventArgs e)
        {
            base.OnMouseDoubleClick(e);
            new CustomMessageBox().ShowDialog();
        } 
    }

    class CustomMessageBox : Window
    {
        public CustomMessageBox()
        {
            Owner = Application.Current.MainWindow;
        }
    }

    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();
        }

        protected override void OnMouseDoubleClick(MouseButtonEventArgs e)
        {
            base.OnMouseDoubleClick(e);
            new SomeDialog() { Owner = this }.Show();
        }
    }

Window1 is the main application window.

SomeDialog is a window that pops up on some event within Window1(double clicking window1 in the example) that needs to be modeless.

CustomMessageBox is a window that pops up on some event within "SomeDialog" (double clicking SomeDialog in the example) that needs to be modal.

If you run the application, and then double click Window1's content to bring up SomeDialog, and then you then double click SomeDialog's content to bring up the CustomMessagebox.

Now you close CustomMessagebox. Fine.

Now if you close SomeDialog, Window1 minimizes? Why is it minimizing and how can I stop it?

Edit : It appears the workaround is rather simple, using the technique suggesrted by Viv.

class SomeDialog : Window
    {
        protected override void OnMouseDoubleClick(MouseButtonEventArgs e)
        {
            base.OnMouseDoubleClick(e);
            new CustomMessageBox().ShowDialog();
        }

        protected override void OnClosing(System.ComponentModel.CancelEventArgs e)
        {
            base.OnClosing(e);
            Owner = null;
        }
    }
Était-ce utile?

La solution

Why is it minimizing and how can I stop it?

Not sure about the "Why" maybe you can report it as a bug and see what they reply with as with a non-modal dialog you do not expect this to happen.

As for a workaround, Try something like this:

public partial class MainWindow : Window {
  ...

  protected override void OnMouseDoubleClick(MouseButtonEventArgs e) {
    base.OnMouseDoubleClick(e);
    var x = new SomeDialog { Owner = this };        
    x.Closing += (sender, args) => {
      var window = sender as Window;
      if (window != null)
        window.Owner = null;
    };
    x.Show();
  }
}

^^ This should prevent the MainWindow(parent) from minimizing when SomeDialog is closed.

Autres conseils

My workaround for this interesting problem is to activate the MainWindow once and after that activate the SomeDialog window again.

class SomeDialog : Window
{
    protected override void OnMouseDoubleClick(MouseButtonEventArgs e)
    {
        base.OnMouseDoubleClick(e);
        new CustomMessageBox().ShowDialog();
        Owner.Activate();
        Activate();
    }
}

A very late answer... I just sat having this same issue and Viv's workaround solved it for me aswell. As for the "Why" part of the answer, i believe it happens when your child window spawns a child window of its own in its lifetime. For my experience it occured whenever pressing my Save button, which is in a flow which requires a child window to be opened. But pressing the Cancel (or escape) or the windows default quit button did not invoke the issue.

First, as your code stands, I can confirm this strange behaviour. There are two things that I noticed here. The first is that the SomeDialog.Owner is not set, or that it ends up with a null value with this code:

new SomeDialog() { Owner = this }.Show();

Adding this code fixes that problem:

public SomeDialog()
{
    Owner = Application.Current.MainWindow;
}

Unfortunately, this doesn't stop the MainWindow from being minimised when the child Window is closed. Then I found that I could stop it from being minimised, but only when calling new SomeDialog().ShowDialog(); instead of new SomeDialog().Show(); However, that makes this Window into a dialog, which is not what you're after I believe.

We had similar problem, but cause was quite simple. Method Close() of Window was called twice. After we had removed second call, all got back to normal.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top