一旦你创建了你的用户控件,它就被创建了,你可以将它分配给另一个 ElementHost(我的例子,两个表单都有元素控件)。看看这是否符合您的要求。
表格1
public partial class Form1 : Form
{
Form2 frm2;
bool popout = false;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
if (popout)
{
frm2.Close();
}
else
{
frm2 = new Form2();
frm2.FormClosed += new FormClosedEventHandler(frm2_FormClosed);
this.elementHost1.Child = null;
frm2.setUserControl(userControl11);
frm2.Show();
popout = true;
}
}
void frm2_FormClosed(object sender, FormClosedEventArgs e)
{
frm2.setUserControl(null);
this.elementHost1.Child = userControl11;
frm2.FormClosed -= new FormClosedEventHandler(frm2_FormClosed);
popout = false;
}
}
表格2
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
public void setUserControl( System.Windows.Controls.UserControl item)
{
elementHost1.Child = item ;
}
}