If you want hide / show behavior, you should not call Window.Close () at all in the window, but hide it by calling Window.Hide (). If the user has closed it, although closing is inevitable, you can try the following. Flip OnClosing inside the window and set e.Cancelled to true, then call .Hide (). This should allow the window to be hidden / displayed even if the user closes the window.
// Disclaimer, untested! protected override void OnClosing(CancelEventArgs e) { e.Cancel = true; // cancels the window close this.Hide(); // Programmatically hides the window }
EDIT:
Ok, I read your question right now ;-) So, how can I start a new window after closing or hide the window.
When you re-show the window using the above, this, of course, will be the same instance that was previously hidden, therefore, will have the same data and state. If you want completely new content, you need to create a new window () and call Window.Show (). If you hide / show as above, you will get the window back in exactly the same state until it is hidden.
Are you using the MVVM pattern in your WPF application? If so, you can try the following. Having all the data in the ViewModel and attached to the view (that is: there is no business logic or data in the code behind the window), you can call the method on the ViewModel to reset all the data when this window is shown. Your bindings will be updated, and the state of the window will be reset. Please note that this will only work if you correctly executed MVVM and linked all the elements of the main form with the ViewModel properties (including auxiliary controls).
Yours faithfully,
source share