【问题标题】:c# winform : override default properties in designer from custom controlc# winform:从自定义控件覆盖设计器中的默认属性
【发布时间】:2015-12-21 21:28:29
【问题描述】:

情况是这样的。我做了一个自定义按钮控件:

public partial class EButton : Control, IButtonControl

此控件包含一个按钮。我使用 getter/setter 在设计器中编辑他的属性,如下所示:

[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
[Browsable(true)]
public UIButton Button
{
    get
    {
        return EBtn;
    }
    set
    {
        EBtn = value;
    }
}

现在,我可以在设计器中访问我的所有按钮属性。

我的问题是,无论我定义什么,它总是被我控件中的默认属性覆盖。

示例: 在我的控制中,按钮的 BackColor 设置为白色。 在特定的表单中,我希望此按钮为红色,因此我在表单的设计器中将 BackColor 属性设置为红色。 当我重新加载设计器时,该值已返回到 White。

我不想为按钮的每个属性创建一个设置器。这是一个特定的控件 (http://www.janusys.com/controls/),它有很多有用的属性,我想针对每种特定情况进行调整。

有人知道解决办法吗?

【问题讨论】:

标签: c# winforms properties controls windows-forms-designer


【解决方案1】:

你应该使用[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]

[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
public Button MyButtonProperty
{
    get
    {
        return this.button1;
    }
    set
    {
        value = this.button1;
    }
}

DesignerSerializationVisibilityDesignerSerializationVisibility.Content 一起使用,表明该属性由Content 组成,它应该为每个分配给该属性的对象的公共而非隐藏属性生成初始化代码。

这是一个独立的测试:

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;
            }
        }
    }
}

【讨论】:

  • 差不多了。我用了你说的,现在我所有的属性都被重置了。我需要的是使用控件设计器中定义的默认值,并可以在表单设计器中更改它们
  • @Robouste 我用这种方式测试过解决方案,首先创建一个UserControl 然后在上面放一个按钮,然后添加属性,然后重建解决方案,然后把你的UserControl 放在一个表单,然后设置它的BackColor,然后关闭并重新打开表单,属性保持不变。
  • 是的,你是对的......现在我必须检查我所有的项目以重新启动按钮,而且它们很多......感谢您的回答,它非常有帮助
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-10-03
  • 1970-01-01
  • 1970-01-01
  • 2011-05-16
  • 2015-08-02
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多