Why is the winforms designer ignoring attributes on overriden properties?
I've got a user control defined like this :
public partial class FooControl : UserControl
{
private System.Windows.Forms.GroupBox groupBox1;
...
I wanted to make groupBox1.Text accessible directly from the designer so I went for the obvious solution and created the following property in my FooControl :
[CategoryAttribute("Appearance"), DescriptionAttribute("The text associated with this control.")]
public string Text
{
get { return groupBox1.Text; }
set { groupBox1.Text = value;}
}
This doesn't work because Text is already defined in my super class (in fact, it's a bit in the dark because of the browseable=false attribute, but I eventually found it) :
public class UserControl : ContainerControl
{
[Bindable(false)]
[EditorBrowsable(EditorBrowsableState.Never)]
[Browsable(false)]
[DesignerSerializationVisibility(DesignerSe开发者_开发技巧rializationVisibility.Hidden)]
public override string Text { get; set; }
An easy workaround is to use "Text2" instead of "Text" as the property name, and in this case everything works fine.
However, if I use override or new, my code compile (and works) but my Text property is not visible in the designer.
What's the reason for this behavior ? Is there a workaround (other than using another property name) ?
I know that is is a very old question and you may have solved this already, but I have found that this works and I wanted to post it for anyone who could benefit:
public partial class FooControl : UserControl
{
string m_text;
[CategoryAttribute("Appearance"), DescriptionAttribute("The text associated with this control.")]
[Bindable(true)]
[EditorBrowsable(EditorBrowsableState.Always)]
[Browsable(true)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
public override string Text
{
get { return m_text; }
set
{
m_text = value;
groupBox1.Text = m_text;
}
}
public FooControl()
{
InitializeComponent();
}
}
Note, I have not tested the above code in Visual Studio 2008, but it should work without issue.
精彩评论