我已经构建了非常简单的用户控件,其中包含 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 个面板的所有属性?现在我可以点击其中的每一个并更改每个属性。