c# winform : override default properties in design

2019-08-11 14:58发布

问题:

Here is the situation. I made a custom button control :

public partial class EButton : Control, IButtonControl

This control contains a button. I use getters/setters to edit his properties in designer, like this :

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

Now, I can access all of my button's properties in the designer.

My problem is that no matter what I define, this is always overridden by the default properties in my control.

Example : In my control, the BackColor of the button is set to white. In a specific form, I want this button red, so I set BackColor properties to red in the designer of the form. When I then reload the designer, the value has returned to White.

I don't want to make a setters for each property of the button. This is a specific control (http://www.janusys.com/controls/) and it has A LOT of useful properties which I want to adapt for each specific situation.

Does anyone know a solution ?

回答1:

You should use [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]

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

Using the DesignerSerializationVisibility with DesignerSerializationVisibility.Content, you are indicating that the property consists of Content, which should have initialization code generated for each public, not hidden property of the object assigned to the property.

Here is a stand alone test:

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