I am using a window to change database configuration for my application. Settings button click is handeled by Config(), it show settings windows when clicked. If I close the using cross sign on the right corner of window, I am not able to reuse the windows it shows following exception.
Cannot set Visibility or call Show, ShowDialog, or WindowInteropHelper.EnsureHandle after a Window has closed.
ConfigDialogBox configDlg = new ConfigDialogBox();
private void SettingsChanged(object sender, RoutedEventArgs e)
{
Database.host = configDlg.host;
Database.port = configDlg.port;
Database.user = configDlg.user;
Database.password = configDlg.password;
Database.database = configDlg.database;
ConfigDlg.Visibility = Visibility.Hidden;
}
private void Config(object sender, RoutedEventArgs e)
{
configDlg.Show();
configDlg.okButton.Click+=new RoutedEventHandler(SettingsChanged);
configDlg.cancelButton.Click+=new RoutedEventHandler(SettingsChanged);
}
void cancel_Click(object sender, RoutedEventArgs e)
{
ConfigDlg.Visibility = Visibility.Hidden;
}
How can I reuse it 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.Closingevent (see the link for an example).