C# 使用TableLayoutPanel作为容器的用户控件

C# 使用TableLayoutPanel作为容器的用户控件,c#,winforms,windows-forms-designer,C#,Winforms,Windows Forms Designer,我有一个用户控件,它充当其他控件的容器。My container非常简单,因为它只包含一个样式化的面板,其中包含一个TableLayoutPanel来容纳多个子控件: MyContainerControl -> Panel -> TableLayoutPanel 现在我想为嵌套控件启用设计器支持。快速搜索表明这是可能的: 但是,我的控件与示例中的控件略有不同。示例控件将子控件添加到面板,而我的控件将子控件添加到TableLayoutPanel(通常是Panel的子类) 我认

我有一个用户控件,它充当其他控件的容器。My container非常简单,因为它只包含一个样式化的面板,其中包含一个
TableLayoutPanel
来容纳多个子控件:

MyContainerControl -> Panel -> TableLayoutPanel
现在我想为嵌套控件启用设计器支持。快速搜索表明这是可能的:

但是,我的控件与示例中的控件略有不同。示例控件将子控件添加到面板,而我的控件将子控件添加到TableLayoutPanel(通常是Panel的子类)

我认为这应该不是什么大问题,并开始实施:

[Designer(typeof(ControlListPanel.Designer))]
public partial class ControlListPanel : UserControl
{
    [Browsable(false)]
    [EditorBrowsable(EditorBrowsableState.Never)]
    [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
    public TableLayoutPanel LayoutArea
    {
        get { return this.rootTableLayoutPanel; }
    }

    // ... more class content ...

    public class Designer : ParentControlDesigner
    {
        public override void Initialize(System.ComponentModel.IComponent component)
        {
            base.Initialize(component);

            if (this.Control is ControlListPanel)
            {
                this.EnableDesignMode(((ControlListPanel)this.Control).LayoutArea, "LayoutArea");
            }
        }
    }
}
现在我可以将控件拖放到用户控件上,但查看设计器文件会发现该控件已添加到用户控件本身:

this.controlListPanel1.Controls.Add(this.myChildControl);
我希望将其添加到表布局中:

this.controlListPanel1.LayoutArea.Controls.Add(this.myChildControl);
当我按如下方式更改
LayoutArea
属性时,设计器将执行我所期望的操作,并将子控件添加到我的
LayoutArea
属性中:

[Browsable(false)]
[EditorBrowsable(EditorBrowsableState.Never)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
public Panel LayoutArea
{
    get { return this.panel; }
}
(我返回面板而不是表格布局)


我有什么遗漏吗?为什么设计师不想将我的子控件添加到
TableLayoutPanel

当TLP嵌套在UC中时,不支持将其置于设计模式。不是完全重复,但根本原因是:@HansPassant,这很糟糕。你会看到什么解决办法吗?据我所知,我可以在没有设计师支持和根本不使用TLP之间做出决定,对吗?嗯。@Hans:我想知道这个想法的症结何在:把TLP放在其他地方,在那里你可以在设计师那里设计它,直到你满意为止,然后在运行时(!)将它移动到你的目标面板上。。?我尝试了一个额外的表单,但是一个隐藏的标签也应该可以使用;private void testButton_Click(object sender,EventArgs e){F2=new Form2();targetPanel.Controls.Add(F2.TLP);F2.Close();}其中F2.TLP是Form2上的公共属性,FLP在F2的构造函数中被分配到该属性。@TaW-设计器将设计序列化为代码。在本例中,该代码已经存在,并锁定在UserControl中,您不能将其放在“其他地方”,也不能对其进行修改。区分来自UC的旧代码和需要添加的新代码的需要无疑是TLP的障碍。