UserControl 或 Control 的名称属性

发布于 2024-07-25 16:38:22 字数 96 浏览 2 评论 0原文

Control 或 UserControl 的 Name 属性有什么特别之处,导致它在 Visual Studio 的属性网格内显示为“(Name)”?

What is special about the Name property of a Control or UserControl that causes it to be displayed as "(Name)" inside the property grid within Visual Studio?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

末蓝 2024-08-01 16:38:22

请查看这篇有关 .NET 中设计时属性的文章。 具体来说,我认为您正在寻找 Browsable 属性,该属性在 Visual Studio 的设计时属性对话框中启用属性。

如果您有一个名为 Name 的属性,则可以这样声明它:

[Browsable(true)]
public string Name { /*...*/ }

您可以设置更多属性,例如 DescriptionDefaultValueCategory,如果您计划向其他开发人员展示您的控件,它将派上用场。

编辑:要获得您想要的效果,请同时使用 BrowsableParenthesizePropertyName 属性:(

[Browsable(true)]
[ParenthesizePropertyName(true)]
public string Name { /*...*/ }

感谢 Ksempac 的评论。)

因为您没有指定 if你正在使用 VB 或 C#,VB 中也有同样的事情:

<Browsable(true)> _
<ParenthesizePropertyName(true)> _
Public Property Name(Value As String) As String
' ...
End Property

编辑 2:

我想你想知道为什么你想首先用括号将你的属性括起来,或者属性名称的含义是什么?它周围有括号。

您可以在此处找到答案:

带括号的属性显示在窗口顶部 - 如果列表按类别分组,则显示在其类别的顶部

基本上,如果某个属性很重要,您希望它出现在排序列表的顶部,因此您将其包围用括号来表明这一点。

Check out this article about design-time attributes in .NET. Specifically, I think you are looking for the Browsable attribute, which enables properties in Visual Studio's design-time properties dialogue.

If you have a property called Name, you'd declare it like this:

[Browsable(true)]
public string Name { /*...*/ }

You can set a lot more attributes, like Description, DefaultValue and Category, which will come in handy if you're planning on presenting your controls to other developers.

EDIT: To get the effect that you want, use both the Browsable and ParenthesizePropertyName attributes:

[Browsable(true)]
[ParenthesizePropertyName(true)]
public string Name { /*...*/ }

(Thanks to Ksempac from the comments for this.)

Since you didn't specify if you're using VB or C#, here's the same thing in VB:

<Browsable(true)> _
<ParenthesizePropertyName(true)> _
Public Property Name(Value As String) As String
' ...
End Property

EDIT 2:

I think you're wondering about why you would want to surround your property with parentheses in the first place, or perhaps what it means for a property's name to have parentheses around it.

You can find the answer to that here:

Parenthesized properties are shown at the top of the window — or at the top of their category if the list is grouped by category

Basically, if a property is important, you want it to appear at the top of a sorted list, so you surround it with parentheses to indicate this.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文