【发布时间】:2009-06-23 12:33:21
【问题描述】:
Control 或 UserControl 的 Name 属性有什么特别之处,导致它在 Visual Studio 的属性网格中显示为“(Name)”?
【问题讨论】:
标签: visual-studio properties propertygrid
Control 或 UserControl 的 Name 属性有什么特别之处,导致它在 Visual Studio 的属性网格中显示为“(Name)”?
【问题讨论】:
标签: visual-studio properties propertygrid
查看this article about design-time attributes in .NET。具体来说,我认为您正在寻找 Browsable 属性,它可以在 Visual Studio 的设计时属性对话框中启用属性。
如果您有一个名为 Name 的属性,您可以这样声明它:
[Browsable(true)]
public string Name { /*...*/ }
您可以设置更多属性,例如Description、DefaultValue 和Category,如果您打算向其他开发人员展示您的控件,这些属性会派上用场。
编辑:要获得您想要的效果,请同时使用 Browsable 和 ParenthesizePropertyName 属性:
[Browsable(true)]
[ParenthesizePropertyName(true)]
public string Name { /*...*/ }
(感谢 cmets 的 Ksempac。)
由于您没有指定您使用的是 VB 还是 C#,所以在 VB 中也是如此:
<Browsable(true)> _
<ParenthesizePropertyName(true)> _
Public Property Name(Value As String) As String
' ...
End Property
编辑2:
我想您想知道为什么首先要用括号括住您的属性,或者属性名称周围有括号意味着什么。
你可以找到答案here:
带括号的属性显示在窗口的顶部——如果列表按类别分组,则显示在其类别的顶部
基本上,如果一个属性很重要,您希望它出现在排序列表的顶部,因此用括号括起来表示这一点。
【讨论】: