2011-05-05 43 views
1

我想显示自定义对话框多次使用下面的代码:未能显示自定义WPF对话框超过一次

 TestWindow newTestWindow = new TestWindow(test); 
     newTestWindow.Owner = this; 
     newTestWindow.ShowDialog(); 

,我去运行上面的代码时,出现以下异常第2次:

Specified element is already the logical child of another element. Disconnect it first. 

回答

1

机会是你想在两个对话框中显示相同的元素(可能是测试参数)?当它关闭时,您需要从对话框中断开该元素,以便它可以在任何后续对话框中使用。

+0

绝对!你是对的,谢谢。我认为,当GC删除这些对象时,所有这些对象都会自动断开连接。 – Vitalij 2011-05-06 07:56:45

0

正常工作:

public partial class MainWindow : Window 
{ 
    private Test _newTestWindow; 

    public MainWindow() 
    { 
     InitializeComponent(); 

     Loaded += new RoutedEventHandler(OnLoaded); 
    } 

    private void OnLoaded(object sender, RoutedEventArgs e) 
    { 
     _newTestWindow = new Test { Owner = this }; 
     _newTestWindow.ShowDialog(); 

     _newTestWindow = new Test { Owner = this }; 
     _newTestWindow.ShowDialog(); 
    } 
}