【问题标题】:Name property of UserControl or ControlUserControl 或 Control 的 Name 属性
【发布时间】:2009-06-23 12:33:21
【问题描述】:

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

【问题讨论】:

    标签: visual-studio properties propertygrid


    【解决方案1】:

    查看this article about design-time attributes in .NET。具体来说,我认为您正在寻找 Browsable 属性,它可以在 Visual Studio 的设计时属性对话框中启用属性。

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

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

    您可以设置更多属性,例如DescriptionDefaultValueCategory,如果您打算向其他开发人员展示您的控件,这些属性会派上用场。

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

    [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

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

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

    【讨论】:

    • 你的链接很好,但我想他需要同时设置 Browsable 和 ParenthesizePropertyName 才能得到他需要的东西:他用括号指定了名称。
    • 请查看已编辑的问题。我在问它有什么特长。
    • @nils_gate:我不明白你所说的“专业”是什么意思。
    • @nils_gate:查看我的最新编辑。我想这就是你的意思。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-07-17
    • 2010-10-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-03
    • 1970-01-01
    相关资源
    最近更新 更多