2

我已经构建了非常简单的用户控件,其中包含 TableLayoutPanel。我已经创建了布局在此处输入图像描述

我的想法是让控件成为所有其他控件和表单的布局(模板)。
中间列是固定的,但第一列和第三列可以固定或自动调整大小。

我现在需要的是添加设计时支持以允许用户仅向这 4 个面板添加控件。我试过添加:

[Designer("System.Windows.Forms.Design.ParentControlDesigner, System.Design", typeof(IDesigner))]

但这样我只能移动控件,它们不会添加到我的面板中。

到目前为止我的代码:

 [Designer("System.Windows.Forms.Design.ParentControlDesigner, System.Design", typeof(IDesigner))]
    public partial class CustomGrid : UserControl
    {
        public CustomGrid()
        {
            InitializeComponent();
        }

        private bool _firstFixed = true;
        [Browsable(true)]
        [Category("Grid")]
        public bool FirstFixed
        {
            get { return _firstFixed; } 
            set { _firstFixed = value; ReLayout();}
        }

        private float _firstSize = 200F;
        [Browsable(true)]
        [Category("Grid")]
        public float FirstSize
        {
            get { return _firstSize; }
            set { _firstSize = value; ReLayout(); }
        }

        private float _secondSize = 200F;
        [Browsable(true)]
        [Category("Grid")]
        public float SecondSize
        {
            get { return _secondSize; }
            set { _secondSize = value; ReLayout(); }
        }

        private bool _thirdFixed = true;
        [Browsable(true)]
        [Category("Grid")]
        public bool ThirdFixed
        {
            get { return _thirdFixed; }
            set { _thirdFixed = value; ReLayout(); }
        }

        private float _thirdSize = 200F;
        [Browsable(true)]
        [Category("Grid")]
        public float ThirdSize
        {
            get { return _thirdSize; }
            set { _thirdSize = value; ReLayout(); }
        }


        private void ReLayout()
        {
            grid.ColumnStyles.Clear();
            grid.ColumnStyles.Add(_firstFixed ? new ColumnStyle(SizeType.Absolute, _firstSize) : new ColumnStyle(SizeType.Percent, 33.33F));
            grid.ColumnStyles.Add(new ColumnStyle(SizeType.Absolute, _secondSize));
            grid.ColumnStyles.Add(_thirdFixed ? new ColumnStyle(SizeType.Absolute, _thirdSize) : new ColumnStyle(SizeType.Percent, 33.33F));
        }
    }

设计师代码:

partial class CustomGrid
{
    /// <summary> 
    /// Required designer variable.
    /// </summary>
    private System.ComponentModel.IContainer components = null;

    /// <summary> 
    /// Clean up any resources being used.
    /// </summary>
    /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
    protected override void Dispose(bool disposing)
    {
        if (disposing && (components != null))
        {
            components.Dispose();
        }
        base.Dispose(disposing);
    }

    #region Component Designer generated code

    /// <summary> 
    /// Required method for Designer support - do not modify 
    /// the contents of this method with the code editor.
    /// </summary>
    private void InitializeComponent()
    {
        this.grid = new System.Windows.Forms.TableLayoutPanel();
        this.panel1 = new System.Windows.Forms.Panel();
        this.panel2 = new System.Windows.Forms.Panel();
        this.panel3 = new System.Windows.Forms.Panel();
        this.panel4 = new System.Windows.Forms.Panel();
        this.grid.SuspendLayout();
        this.SuspendLayout();
        // 
        // grid
        // 
        this.grid.BackColor = System.Drawing.Color.White;
        this.grid.ColumnCount = 3;
        this.grid.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 200F));
        this.grid.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 200F));
        this.grid.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 100F));
        this.grid.Controls.Add(this.panel1, 0, 0);
        this.grid.Controls.Add(this.panel2, 1, 0);
        this.grid.Controls.Add(this.panel3, 2, 0);
        this.grid.Controls.Add(this.panel4, 1, 1);
        this.grid.Dock = System.Windows.Forms.DockStyle.Fill;
        this.grid.Location = new System.Drawing.Point(0, 0);
        this.grid.Margin = new System.Windows.Forms.Padding(0);
        this.grid.Name = "grid";
        this.grid.RowCount = 2;
        this.grid.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Absolute, 100F));
        this.grid.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 100F));
        this.grid.Size = new System.Drawing.Size(774, 430);
        this.grid.TabIndex = 0;
        // 
        // panel1
        // 
        this.panel1.BackColor = System.Drawing.Color.White;
        this.panel1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
        this.panel1.Dock = System.Windows.Forms.DockStyle.Fill;
        this.panel1.Location = new System.Drawing.Point(5, 5);
        this.panel1.Margin = new System.Windows.Forms.Padding(5);
        this.panel1.Name = "panel1";
        this.grid.SetRowSpan(this.panel1, 2);
        this.panel1.Size = new System.Drawing.Size(190, 420);
        this.panel1.TabIndex = 0;
        // 
        // panel2
        // 
        this.panel2.BackColor = System.Drawing.Color.White;
        this.panel2.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
        this.panel2.Dock = System.Windows.Forms.DockStyle.Fill;
        this.panel2.Location = new System.Drawing.Point(205, 5);
        this.panel2.Margin = new System.Windows.Forms.Padding(5);
        this.panel2.Name = "panel2";
        this.panel2.Size = new System.Drawing.Size(190, 90);
        this.panel2.TabIndex = 1;
        // 
        // panel3
        // 
        this.panel3.BackColor = System.Drawing.Color.White;
        this.panel3.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
        this.panel3.Dock = System.Windows.Forms.DockStyle.Fill;
        this.panel3.Location = new System.Drawing.Point(405, 5);
        this.panel3.Margin = new System.Windows.Forms.Padding(5);
        this.panel3.Name = "panel3";
        this.grid.SetRowSpan(this.panel3, 2);
        this.panel3.Size = new System.Drawing.Size(364, 420);
        this.panel3.TabIndex = 2;
        // 
        // panel4
        // 
        this.panel4.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
        this.panel4.Dock = System.Windows.Forms.DockStyle.Fill;
        this.panel4.Location = new System.Drawing.Point(205, 105);
        this.panel4.Margin = new System.Windows.Forms.Padding(5);
        this.panel4.Name = "panel4";
        this.panel4.Size = new System.Drawing.Size(190, 320);
        this.panel4.TabIndex = 3;
        // 
        // CustomGrid
        // 
        this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        this.Controls.Add(this.grid);
        this.Margin = new System.Windows.Forms.Padding(0);
        this.Name = "CustomGrid";
        this.Size = new System.Drawing.Size(774, 430);
        this.grid.ResumeLayout(false);
        this.ResumeLayout(false);

    }

    #endregion

    private System.Windows.Forms.TableLayoutPanel grid;
    private System.Windows.Forms.Panel panel1;
    private System.Windows.Forms.Panel panel2;
    private System.Windows.Forms.Panel panel3;
    private System.Windows.Forms.Panel panel4;

}

我尝试添加自定义 ControlDesigner(如何为驻留在 UserControl 中的 TabControl 提供设计器支持,以便我可以将控件拖放到标签页上?)但没有任何运气。

我的设计师:

internal class CustomGridControlDesigner : ParentControlDesigner
    {
        public override void Initialize(IComponent component)
        {
            base.Initialize(component);
            var ctl = Control as CustomGrid;
            foreach (object ctrl in ctl.Controls)
            {
                if (ctrl.GetType() != typeof (Panel)) continue;
                var ctrl1 = ctrl as Panel;
                EnableDesignMode(ctrl1, ctrl1.Name);
            }
        }
    }

我的问题是如何添加对将控件拖放到我的控件内的面板的支持?

编辑:

我改变了我的代码:

[Designer(typeof(CustomGridControlDesigner))]
public partial class CustomGrid : UserControl
{
    public CustomGrid()
    {
        InitializeComponent();
    }

    private bool _firstFixed = true;
    [Browsable(true)]
    [Category("Grid")]
    public bool FirstFixed
    {
        get { return _firstFixed; } 
        set { _firstFixed = value; ReLayout();}
    }

    private float _firstSize = 200F;
    [Browsable(true)]
    [Category("Grid")]
    public float FirstSize
    {
        get { return _firstSize; }
        set { _firstSize = value; ReLayout(); }
    }

    private float _secondSize = 200F;
    [Browsable(true)]
    [Category("Grid")]
    public float SecondSize
    {
        get { return _secondSize; }
        set { _secondSize = value; ReLayout(); }
    }

    private bool _thirdFixed = true;
    [Browsable(true)]
    [Category("Grid")]
    public bool ThirdFixed
    {
        get { return _thirdFixed; }
        set { _thirdFixed = value; ReLayout(); }
    }

    private float _thirdSize = 200F;
    [Browsable(true)]
    [Category("Grid")]
    public float ThirdSize
    {
        get { return _thirdSize; }
        set { _thirdSize = value; ReLayout(); }
    }


    [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
    [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
    [Browsable(false), EditorBrowsable(EditorBrowsableState.Never)]
    public Panel Panel1
    {
        get { return this.panel1; }
    }
    [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
    [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
    [Browsable(false), EditorBrowsable(EditorBrowsableState.Never)]
    public Panel Panel2
    {
        get { return this.panel2; }
    }

    [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
    [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
    [Browsable(false), EditorBrowsable(EditorBrowsableState.Never)]
    public Panel Panel3
    {
        get { return this.panel3; }
    }

    [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
    [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
    [Browsable(false), EditorBrowsable(EditorBrowsableState.Never)]
    public Panel Panel4
    {
        get { return this.panel4; }
    }


    private void ReLayout()
    {
        grid.ColumnStyles.Clear();
        grid.ColumnStyles.Add(_firstFixed ? new ColumnStyle(SizeType.Absolute, _firstSize) : new ColumnStyle(SizeType.Percent, 33.33F));
        grid.ColumnStyles.Add(new ColumnStyle(SizeType.Absolute, _secondSize));
        grid.ColumnStyles.Add(_thirdFixed ? new ColumnStyle(SizeType.Absolute, _thirdSize) : new ColumnStyle(SizeType.Percent, 33.33F));
    }
}

还有我的定制设计师:

internal class CustomGridControlDesigner : ParentControlDesigner
{
    public override void Initialize(IComponent component)
    {
        base.Initialize(component);
        EnableDesignMode((Control as CustomGrid).Panel1, "Panel1");
        base.Initialize(component);
        EnableDesignMode((Control as CustomGrid).Panel2, "Panel2");
        base.Initialize(component);
        EnableDesignMode((Control as CustomGrid).Panel3, "Panel3");
        base.Initialize(component);
        EnableDesignMode((Control as CustomGrid).Panel4, "Panel4");
    }
}

现在我可以向这些面板添加控件,但我必须将嵌套面板添加到我的所有面板才能使用对齐工具(蓝线):

在此处输入图像描述

另外,如何隐藏这 4 个面板的所有属性?现在我可以点击其中的每一个并更改每个属性。

4

1 回答 1

1

想法是拥有将成为布局(模板)的控件

您在设计时支持中遇到了众所周知的限制,当它需要处理继承或封装的控件时,它就不能很好地工作了。您还可以很好地创建一个在 Windows 中头重脚轻且在运行时性能不佳的 UI。通过将控件成员声明为private ,您至少在路上创造了一块石头,因为它理所当然地,设计师无法处理它,因为它需要观察可访问性,因此无法生成更改其任何属性的代码。

我强烈建议您改为着手做您实际上打算完成的事情,实际创建一个项目模板。使用 File + Export Template 命令在 Visual Studio 中得到很好的支持。这给用户一个千篇一律的 UserControl 或 Form 开始,不需要自定义设计器。以及他可能希望在需要时调整您的基本设计的所有灵活性,极大地鼓励您的设计的可重用性。也许您不喜欢给用户这种灵活性,创建一个移除功能的设计器比添加它们的设计器容易得多。我不建议您实际这样做,很有可能您会收到一堆要求将它们添加回来的支持请求:)

于 2013-11-12T11:12:58.063 回答