关闭后无法重用wpf窗口

时间:2012-06-27 05:55:42

标签: c# wpf

我正在使用一个窗口来更改我的应用程序的数据库配置。设置按钮单击由Config()进行设置,单击时显示设置窗口。如果我关闭窗口右上角的使用十字标志,我将无法重用它在异常后显示的窗口。

窗口关闭后,无法设置可见性或调用Show,ShowDialog或WindowInteropHelper.EnsureHandle。

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;
        }

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; }

关闭后如何重复使用?

3 个答案:

答案 0 :(得分:2)

您无法重复使用该窗口。

如果通过 OK 取消按钮以外的其他方式关闭窗口是您的问题,则需要处理Window.Closing事件(请参阅相关链接)例)。

答案 1 :(得分:0)

你做不到。只需创建一个新窗口。

答案 2 :(得分:0)

ConfigDialogBox configDlg = null;



private void Config(object sender, RoutedEventArgs e)
        {            
         configDlg = new ConfigDialogBox();
            configDlg.Show();
            configDlg.okButton.Click+=new RoutedEventHandler(SettingsChanged);
            configDlg.cancelButton.Click+=new RoutedEventHandler(SettingsChanged);
        }