Winforms usercontrol在设计时以编程方式添加另一个usercontrol
我有一个usercontrol(UC1),它在设计时根据用户想要显示的内容更改方面Winforms usercontrol在设计时以编程方式添加另一个usercontrol,winforms,user-controls,windows-forms-designer,Winforms,User Controls,Windows Forms Designer,我有一个usercontrol(UC1),它在设计时根据用户想要显示的内容更改方面 一个常规按钮,弹出一个带有usercontrol UC2的窗口(该窗口仅在运行时显示) UC2直接托管在UC1中(此时不显示常规按钮) 因为我想在这两种情况下使用相同的UC2实例,所以我只是在UC1和表单之间转移所有权 public UC1 () { _uc2 = new UC2 (); } public bool DisplayModeSimple { get { return _disp
- 一个常规按钮,弹出一个带有usercontrol UC2的窗口(该窗口仅在运行时显示)
- 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中的任何控件。问题是,您的第一个UserControl托管在VS上,因此它知道处于设计模式。第二个UserControl托管在第一个UserControl中,因此,由于其宿主不是设计器,因此它认为位于普通容器中,并相应地进行操作。如何解决这个问题有点棘手,因为目前还没有一个简单的解决方案。你可以找到一些解决办法。另一种方法是递归地测试Site.DesignMode,但这取决于控件的深度级别。问题是第一个用户控件托管在VS上,因此它知道处于设计模式。第二个UserControl托管在第一个UserControl中,因此,由于其宿主不是设计器,因此它认为位于普通容器中,并相应地进行操作。如何解决这个问题有点棘手,因为目前还没有一个简单的解决方案。你可以找到一些解决办法。另一种可能是递归地测试Site.DesignMode,但这取决于控件的深度。好的,我已经知道测试设计模式,但它没有帮助,因为没有地方可以测试此标志并强制UC2处于“设计模式”。此外,UC1(显示UC2)在设计器中显示宿主窗体时会正常运行。好的,我已经知道测试设计模式,但它没有帮助,因为没有地方可以测试此标志并强制UC2处于“设计模式”。此外,UC1(显示UC2)在设计器中显示宿主窗体时正常工作。