【问题标题】:Move user control from main window to child window then back again to main window?将用户控件从主窗口移动到子窗口,然后再回到主窗口?
【发布时间】:2021-01-27 09:08:19
【问题描述】:

我有一个 MainWindow、一个 userControl 和一个 childWindow。 MainWindow 有一个扩展器,其内容设置为 userControl 的实例,使用 MainWindowXaml.cs 中的以下代码:

public userControl 
userControl1 = new userControl();  
public MainWindow()  
{  
InitializeComponent();  
expander.Content = userControl1;  
}  

然后我将 userControl1 在 childWindow 中单击按钮(该按钮存在于扩展器的标题中):

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 文件中。

【问题讨论】:

  • 在您的 childWindow_closing 方法中,您没有设置 MainWindow.Expander1.Content
  • @WSC 代码在 MainWindowXaml.cs 文件中。
  • MainWindow 构造函数中,您指的是Expander1,但在childWindow_closing 中,您指的是expander。这些是不同的东西。
  • @WSC 抱歉,它实际上应该是扩展器而不是扩展器 1。我会更正这个问题。
  • 当你想将expander.Content = null移到子窗口时,尝试设置。

标签: c# wpf window


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多