Wpf: Cannot Reuse Window After It Has Been Closed

WPF: Cannot reuse window after it has been closed

I suppose you could do it if you changed visibility of the window rather than closing it. You'd need to do that in the Closing() event and then cancel the close. If you allow the close to happen you certainly can't reopen a closed window - from here:

If the Closing event isn't canceled,
the following occurs:

...

Unmanaged resources created by the Window are disposed.

After that happens the window will never be valid again.

I don't think it's worth the effort though - it really isn't that much of a performance hit to make a new window each time and you are far less likely to introduce hard to debug bugs / memory leaks. (Plus you'd need to make sure that it did close and release it's resources when the application is shut down)


Just read that you are using ShowDialog(), this will make the window modal and simply hiding it won't return control to the parent window. I doubt it is possible to do this at all with modal windows.

Cannot reuse wpf window after closing

You can't reuse the window.

If closing the window via something other than OK and Cancel buttons is your problem, you need to handle the Window.Closing event (see the link for an example).

Is there a way to re-use an already closed WPF Window instance

You need to override the wInstance OnClosing method to set the window visibility to hidden and cancel the close event.

 protected override void OnClosing(System.ComponentModel.CancelEventArgs e)
{
this.Visibility = Visibility.Hidden;
e.Cancel = true;
}

Is it possible to re-show and closed dialog window?

Use .Hide() instead of .Close(). That removes it without destroying it. Then you can call Show() again when needed.

 MainWindow test = new MainWindow();
test.Show();
test.Hide();
test.Show();

What happens to a WPF window after it is closed?

Can I detect this state?

As far as I know, there is no way to access this state

Its there a way to reuse this window again other then not closing the window at all and using hide instead?

Yes, handle the Closing event in the dialog window, or override the OnClosing method:

protected override void OnClosing(CancelEventArgs e)
{
e.Cancel = true;
this.Hide();
}


Related Topics



Leave a reply



Submit