User Control as container at design time

前端 未结 3 495
醉梦人生
醉梦人生 2020-12-05 07:24

I\'m designing a simple expander control.

I\'ve derived from UserControl, drawn inner controls, built, run; all ok.

Since an inner Control is a Panel, I\'d l

3条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-05 08:08

    ParentControlDesigner doesn't know what you want do. It only knows you want your UserControl to be a container.

    What you need to do is implement your own designer which enables design mode on the panel:

        using System.ComponentModel;
        using System.Windows.Forms;
        using System.Windows.Forms.Design;
    
        namespace MyCtrlLib
        {
            // specify my custom designer
            [Designer(typeof(MyCtrlLib.UserControlDesigner))]
            public partial class UserControl1 : UserControl
            {
                public UserControl1()
                {
                    InitializeComponent();
                }
    
                // define a property called "DropZone"
                [Category("Appearance")]
                [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)] 
                public Panel DropZone
                {
                    get { return panel1; }
                }
            }
    
            // my designer
            public class UserControlDesigner  : ParentControlDesigner
            {
                public override void Initialize(System.ComponentModel.IComponent component)
                {
                    base.Initialize(component);
    
                    if (this.Control is UserControl1)
                    {
                        this.EnableDesignMode(
                           (UserControl1)this.Control).DropZone, "DropZone");
                    }
                }
            }
        }
    

    I learned this from Henry Minute on CodeProject. See the link for some improvements on the technique.

提交回复
热议问题