编译后,UserControl的文本消失在设计器中 [英] Text of UserControl dissappears in designer after compilation
问题描述
我的解决方案中发生了最奇怪的事情。
我有一个按钮,它是我自定义的。它继承了UserControl,其文本表示为该控件内的Label。
I have the wierdest thing going on in my solution. I have a button, which I customly made. It inherits UserControl, and its text is represented as a Label inside that control.
自然地,我希望覆盖按钮的文本以设置Label的文本:
Naturally, I wanted the button's text to be overridden to set the Label's text:
或者
/// <summary>
/// Gets or sets the text that appears in the button
/// </summary>
[Category("Appearance"), Description("Gets or sets the text on the button.")]
[Browsable(true)]
public override string Text
{
get
{
return base.Text;
}
set
{
base.Text = value;
labelButtonText.Text = value;
}
}
Or
/// <summary>
/// Gets or sets the text that appears in the button
/// </summary>
[Category("Appearance"), Description("Gets or sets the text on the button.")]
[Browsable(true)]
public override string Text
{
get
{
return labelButtonText.Text ;
}
set
{
labelButtonText.Text = value;
}
}
无论使用哪种方法,当我在另一个UserControls / Forms,
在编译后消失在设计器中的文本消失。
Regardless of the method, when I use that button in another UserControls/Forms, the text I explicitly put inside in the designer dissappears after compilation.
我在 Button.designer.cs文件中签入,
I checked in the "Button.designer.cs" file, and there is no assignment of the text to null nor empty for neither the UserControl nor the Label.
EDIT :此外,当我将设计器中的文本属性,不会在* .designer.cs文件中设置。
EDIT: Moreover, when I set the Text property in the designer, it does not set in the *.designer.cs file.
预先感谢。
推荐答案
是的,这是设计使然。 UserControl.Text属性如下所示:
Yes, this is by design. The UserControl.Text property looks like this:
[Browsable(false)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
[EditorBrowsable(EditorBrowsableState.Never)]
[Bindable(false)]
public override string Text
{
get
{
return base.Text;
}
set
{
base.Text = value;
}
}
您照顾了[Browsable]属性,但不是 [DesignerSerializationVisibility]。隐藏是使文本消失的原因。通过撤消所有属性来修复它:
You took care of the [Browsable] attribute but not the [DesignerSerializationVisibility]. Hidden is what makes the text disappear. Fix it by undo-ing all of the attributes:
[Browsable(true)]
[EditorBrowsable(EditorBrowsableState.Always)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
[Bindable(true)]
public override string Text {
// etc..
}
这篇关于编译后,UserControl的文本消失在设计器中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!