【发布时间】:2011-08-14 20:20:42
【问题描述】:
我有一个用户控件 (UC1),它在设计时根据用户想要显示的内容更改外观。
- 一个常规按钮,弹出一个带有用户控件 UC2 的窗口(该窗口仅在运行时显示)
- UC2 直接托管在 UC1 中(常规按钮不显示)
由于我想在这两种情况下使用相同的 UC2 实例,我只是在 UC1 和表单之间转移所有权。
public UC1 ()
{
_uc2 = new UC2 ();
}
public bool DisplayModeSimple
{
get { return _displayModeSimple; }
set
{
_displayModeSimple = value;
if (_displayModeSimple)
{
// ... Verify if _uc2 is already in Controls...
Controls.Remove (_uc2);
uiButton.Visible = true;
}
else
{
// ... Verify that _uc2 is not in Controls ...
Controls.Add (_uc2);
uiButton.Visible = false;
}
}
}
private void HandleButtonClick (object sender, EventArgs e)
{
// Not called if DisplayModeSimple=false since button is hidden...
using (var form = new PopupForm (_uc2))
{
form.ShowDialog (this);
}
}
在设计模式和运行时模式下都能正常工作。
在设计模式下,如果我更改显示模式,UC1 会正常运行。
但是,UC2 上的控件可以像在运行时一样单击。 如果我随后关闭托管 UC1 的表单并重新打开它,一切都会恢复正常,即,我无法“单击”UC2 中的任何控件。
【问题讨论】:
标签: winforms user-controls windows-forms-designer