代码之家  ›  专栏  ›  技术社区  ›  Eric Smith

PropertyGrid中的自定义属性名称?

  •  20
  • Eric Smith  · 技术社区  · 15 年前

    我有一个类,我用在 PropertyGrid CategoryAttribute 显然,它会在每个属性上为每个项目创建一个新类别。这将我的属性网格设置为每个项目都有一个[+],其中包含我的自定义名称,而这不是我试图实现的行为。

    在VisualStudio中,如果单击解决方案资源管理器中的某个项(例如程序集),则它没有树节点,只有一个完全命名的属性列表,即任何字符串都可以标识属性,而不仅仅是对象的名称。因此,与其这样:

    [+文件路径]
    文件路径|属性值

    文件大小| 0 KB

    [+文件]


    甚至上面没有初始[+]节点。我已经在System.ComponentModel命名空间中查找了一个适用的属性,但找不到。

    我怎样才能达到这个效果?这一定是可能的,VisualStudio做到了,我相信它们是同一个组件,而不是派生和扩展的组件。

    2 回复  |  直到 6 年前
        1
  •  43
  •   Tom Anderson    15 年前

    使用 DisplayNameAttribute DescriptionAttribute 将帮助文本添加到属性,以及 CategoryAttribute

    using System.ComponentModel;
    
    [Category("Test")]
    [DisplayName("Test Property")]
    [Description("This is the description that shows up")]
    public string TestProperty {get;set;}
    
        2
  •  9
  •   Reed Copsey    15 年前

    你会想要 CategoryAttribute

    [Category("File")]
    public string FilePath { get; set;}
    
    [Category("File")]
    public int FileSize { get; set;}
    

    我推荐阅读“ Getting the most out of the .NET Property Grid Control