我构建了非常简单的用户控件,其中包含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(How do I provide designer support to a TabControl residing in a UserControl, so that I can drag/drop controls onto tab pages?),但没有任何运气。
我的设计师:
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个面板的所有属性?现在我可以点击它们中的每一个并更改每个属性。
答案 0 :(得分:1)
想法是控制将成为布局(模板)
您在设计时支持方面遇到了众所周知的限制,当它需要处理继承或封装的控件时,它不再能够很好地工作。你也很好地创建了一个窗口比较重的UI,并且在运行时性能很差。你通过声明控件成员 private 来创建至少一个摇滚乐,因为它是理所当然的,设计师无法处理它,因为它需要观察可访问性,因此无法生成更改其任何属性的代码。
我强烈建议你改为开始做你实际想要完成的事情,实际创建一个项目模板。使用File + Export Template命令在Visual Studio中得到了相当好的支持。这为用户提供了一个千篇一律的UserControl或Form,开始时不需要定制设计师。并且他可能希望在需要时调整您的基础设计的所有灵活性,极大地鼓励您的设计的可重用性。也许你不喜欢给用户这种灵活性,创建一个设计师删除功能比添加它们容易得多。我不建议你真的这样做,很有可能你得到一堆支持请求,要求添加它们:)