2

I have a WPF MainWindow where I can open a second one as follow, taking as an example this post:

var win = new CalibrationWindow(){Owner = this};
win.ShowDialog();

Rarely happens that I have the following runtime exception "cannot set owner property to a window that has not been shown previously".

Do you have any suggestions? Thanks in advance.

Community
  • 1
  • 1
ctt_it
  • 339
  • 1
  • 6
  • 22

1 Answers1

0

Handle the StateChanged or Activated event of the window and set the Owner property in this event handler as suggested here:

Having Trouble Setting Window's Owner in Parent's Constructor

var win = CalibrationWindow();
this.Activated += (s, e) => { win.Owner = this; };
win.ShowDialog();
Community
  • 1
  • 1
mm8
  • 163,881
  • 10
  • 57
  • 88
  • What if I can open several Windows? If I do the following private void MainWindow_OnActivated(object sender, EventArgs e) { ((Window) sender).Owner = this; } I have an eception when I open the window at the first time. – ctt_it Feb 06 '17 at 13:59
  • The parent window *must* have been opened before you can use it as an owner of the child window. – mm8 Feb 06 '17 at 14:01
  • Sure, the MainWindow is shown otherwise I can't press the button in order to show the second window. – ctt_it Feb 06 '17 at 14:09