将用户控件从 main window 移动到 child window 然后再回到 main window?

Move user control from main window to child window then back again to main window?

我有一个主窗口、一个用户控件和一个子窗口。 MainWindow 有一个扩展器,其内容设置为 userControl 的实例,使用 MainWindowXaml.cs:

中的以下代码
public userControl 
userControl1 = new userControl();  
public MainWindow()  
{  
InitializeComponent();  
expander.Content = userControl1;  
}  

然后我在单击按钮时将 userControl1 放入子窗口(该按钮出现在扩展器的 header 中):

private void b_click(...)  
{  
Window window = new Window();  
window.Content = userControl1;  
window.Closing += childWindow_closing;
window.Show();  
expander.Visibility = Visibility.Collapsed;  
}  

有效。

现在我尝试在 childWindow 关闭事件期间将 userControl1 放回扩展器:

private void childWindow_closing(...)  
{  
expander.Visibility = Visibility.Visible;  
expander.Content = userControl1;  
}  

但是,这是行不通的。 childWindow 关闭并显示扩展器。但是 userControl1 没有显示在扩展器中。关于原因的任何线索?

注意:上述所有代码都在 MainWindowXaml.cs 文件中。

正如@WSC 建议设置 expander.Content = null;解决了这个问题。