使用c#WinForm为内部面板创建DesignMode

时间:2013-09-19 13:43:51

标签: c# .net controls

我的控件中有2个面板。

面板3添加到面板1的Title, 面板2添加到面板1 For Content& 面板1是我的控件

我为我的项目创建了这个控件。 本控制代码:

public class ControWithTitle : Panel // Panel 1
{
    public Panel Title = new Panel { Dock = DockStyle.Top, Height = 20, BackColor = Color.Black }; // Panel 2
    public Panel Content = new Panel { Dock = DockStyle.Fill, BackColor = Color.White }; // Panel 3
    public ControWithTitle ()
        : base()
    {

        this.Controls.Add(Title);
        this.Controls.Add(Content);
    }
}

我在Form>中添加此控件时执行此操作为内容创建设计模式。不是面板1或标题面板......

此代码无效...所有面板均为锁定。在Build Project之后,每个Changed都被重置......

这是真的吗?如何创建这个?

 ╔═══════════╗ < Paenl 1
 ║╔═════════╗║
 ║║ Panel 2 ║║
 ║╚═════════╝║
 ║╔═════════╗║
 ║║         ║║
 ║║         ║║
 ║║         ║║
 ║║ Panel 3 ║║ < Design Mode For This Paenl
 ║║         ║║
 ║║         ║║
 ║║         ║║
 ║╚═════════╝║
 ╚═══════════╝
粘贴后亲爱的@ King's Code我的问题没解决了...... 这个错误出现在我的项目中:

enter image description here

这是我的其他样本代码(@Hans Passant&amp; @King King)答案:

    [Designer(typeof(CustomDesigner))]
public partial class ControlWithTitle : UserControl // Panel 1
{
    private System.ComponentModel.IContainer components = null;
    private ListView listView1 = new ListView();
    protected override void Dispose (bool disposing)
    {
        if (disposing && (components != null)) { components.Dispose(); }
        base.Dispose(disposing);
    }
    private void InitializeComponent ()
    {
        components = new System.ComponentModel.Container();
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        this.Controls.Add(listView1);
    }
    public Panel Title = new Panel { };
    public Panel Content = new Panel { };

    [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
    public ListView Employees { get { return listView1; } }

    public ControlWithTitle ()
    {
        InitializeComponent();
    }
}
public class CustomDesigner : ParentControlDesigner
{
    public override void Initialize (IComponent component)
    {
        ControlWithTitle control = Control as ControlWithTitle;
        if (control != null)
        {
            EnableDesignMode(control.Employees, "Employees");
        }
    }
}

1 个答案:

答案 0 :(得分:1)

您可以尝试使用此代码,您必须添加对System.Design.dllusing System.Windows.Forms.Design;的引用:

[Desiner(typeof(CustomDesigner))]
public class ControWithTitle : Panel // Panel 1
{
   //....
}
public class CustomDesigner : ParentControlDesigner {
   public override void Initialize(IComponent component){
      ControWithTitle control = Control as ControWithTitle;
      if(control != null){
        //Enable designmode for Panel3
        EnableDesignMode(control.Content, "Content");
      }
   }
}