c# winform:从自定义控件覆盖设计器中的默认属性
c# winform : override default properties in designer from custom control
情况是这样的。我做了一个自定义按钮控件:
public partial class EButton : Control, IButtonControl
此控件包含一个按钮。我使用 getters/setters 在设计器中编辑他的属性,像这样:
[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
[Browsable(true)]
public UIButton Button
{
get
{
return EBtn;
}
set
{
EBtn = value;
}
}
现在,我可以在设计器中访问按钮的所有属性。
我的问题是,无论我定义什么,它总是被我控件中的默认属性覆盖。
示例:
在我的控件中,按钮的 BackColor 设置为白色。
在特定的窗体中,我希望这个按钮是红色的,所以我在窗体的设计器中将BackColor 属性设置为红色。
当我重新加载设计器时,该值已返回到白色。
我不想为按钮的每个 属性 创建一个设置器。这是一个特定的控件 (http://www.janusys.com/controls/),它有很多有用的属性,我想针对每种特定情况进行调整。
有人知道解决办法吗?
你应该使用[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
public Button MyButtonProperty
{
get
{
return this.button1;
}
set
{
value = this.button1;
}
}
使用 DesignerSerializationVisibility
和 DesignerSerializationVisibility.Content
,表示 属性 包含内容,每个 public 都应该生成初始化代码,而不是隐藏 属性 的对象分配给 属性.
这是一个独立测试:
using System.ComponentModel;
using System.Windows.Forms;
namespace MyControls
{
public partial class MyUserControl : UserControl
{
private System.ComponentModel.IContainer components = null;
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Component Designer generated code
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(3, 15);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(75, 23);
this.button1.TabIndex = 0;
this.button1.Text = "button1";
this.button1.UseVisualStyleBackColor = true;
//
// MyUserControl
//
this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 16F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.Controls.Add(this.button1);
this.Name = "MyUserControl";
this.ResumeLayout(false);
}
#endregion
private System.Windows.Forms.Button button1;
public MyUserControl()
{
InitializeComponent();
}
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
public Button MyButtonProperty
{
get
{
return this.button1;
}
set
{
value = this.button1;
}
}
}
}
情况是这样的。我做了一个自定义按钮控件:
public partial class EButton : Control, IButtonControl
此控件包含一个按钮。我使用 getters/setters 在设计器中编辑他的属性,像这样:
[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
[Browsable(true)]
public UIButton Button
{
get
{
return EBtn;
}
set
{
EBtn = value;
}
}
现在,我可以在设计器中访问按钮的所有属性。
我的问题是,无论我定义什么,它总是被我控件中的默认属性覆盖。
示例: 在我的控件中,按钮的 BackColor 设置为白色。 在特定的窗体中,我希望这个按钮是红色的,所以我在窗体的设计器中将BackColor 属性设置为红色。 当我重新加载设计器时,该值已返回到白色。
我不想为按钮的每个 属性 创建一个设置器。这是一个特定的控件 (http://www.janusys.com/controls/),它有很多有用的属性,我想针对每种特定情况进行调整。
有人知道解决办法吗?
你应该使用[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
public Button MyButtonProperty
{
get
{
return this.button1;
}
set
{
value = this.button1;
}
}
使用 DesignerSerializationVisibility
和 DesignerSerializationVisibility.Content
,表示 属性 包含内容,每个 public 都应该生成初始化代码,而不是隐藏 属性 的对象分配给 属性.
这是一个独立测试:
using System.ComponentModel;
using System.Windows.Forms;
namespace MyControls
{
public partial class MyUserControl : UserControl
{
private System.ComponentModel.IContainer components = null;
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Component Designer generated code
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(3, 15);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(75, 23);
this.button1.TabIndex = 0;
this.button1.Text = "button1";
this.button1.UseVisualStyleBackColor = true;
//
// MyUserControl
//
this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 16F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.Controls.Add(this.button1);
this.Name = "MyUserControl";
this.ResumeLayout(false);
}
#endregion
private System.Windows.Forms.Button button1;
public MyUserControl()
{
InitializeComponent();
}
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
public Button MyButtonProperty
{
get
{
return this.button1;
}
set
{
value = this.button1;
}
}
}
}