如何设置设计时属性的默认值? [英] How to set design-time property default values?
问题描述
根据MSDN(http://msdn.microsoft.com/en-us/library/system.windows.forms.label.autosize.aspx ),其中有关于 Label
的 AutoSize
属性的注释:
According to MSDN (http://msdn.microsoft.com/en-us/library/system.windows.forms.label.autosize.aspx), there's a note about Label
's AutoSize
property:
使用设计器添加到表单时,默认值为true.从代码实例化时,默认值为false.
When added to a form using the designer, the default value is true. When instantiated from code, the default value is false.
问题是:如何覆盖 Label
控件并将其 AutoSize
属性的 design-time 默认值设置为 false
?
The question is: how can I override a Label
control and set its AutoSize
property's design-time default value to false
?
(更新)
这不起作用:
class MyLabel : Label
{
const bool defaultAutoSize = false;
public MyLabel()
{
AutoSize = defaultAutoSize;
}
[DefaultValue(defaultAutoSize)]
public override bool AutoSize
{
get
{
return base.AutoSize;
}
set
{
base.AutoSize = value;
}
}
}
推荐答案
Label
控件具有一个属性:
[ToolboxItem("System.Windows.Forms.Design.AutoSizeToolboxItem,System.Design, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")]
这会导致奇怪的 AutoSize
问题.
我可以通过以下方式禁用它:
I can disable it by this:
[ToolboxItem(true)]
class MyLabel : Label
{
}
这篇关于如何设置设计时属性的默认值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!