C# - 如何在设计器中更改属性的默认值?

C# - 如何在设计器中更改属性的默认值?

问题描述:

在用户控件中,我有一个属性Text Text,其定义如下:

In a user control I have a property Text Text with definition as follows :

string text;
[Browsable(true), EditorBrowsable(EditorBrowsableState.Always)]
public override string Text
{
    get
    {
        return text;
    }
    set
    {
        this.text = value;
        this.Invalidate();
    }
}



我将此usercontrol添加到表单并在Properties选项卡中编辑了Text属性。

当我这样做时,usercontrol会在其上显示新文本。

然后当我运行它时,usercontrol没有任何文本,只是没有。



我尝试过:



我尝试添加


I added this usercontrol to a form and edited the Text property in the Properties tab.
When I do this the usercontrol shows the new text on it.
Then when I run it , the usercontrol does not have any text , just nothing.

What I have tried:

I have tried adding

DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]



以及


along with

[Browsable(true), EditorBrowsable(EditorBrowsableState.Always)]

我刚刚找到解决方案。

对于那些偶然发现此页面的人来说如下:

I just found the solution.
For those who stumble on this page the solution is given below :
string text;
[EditorBrowsable(EditorBrowsableState.Always)]
[Browsable(true)]	[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
[Bindable(true)]
public override string Text
{
    get
    {
        return text;
    }
    set
    {
        this.text = value;
        this.Invalidate();
    }
}