Custom property names in PropertyGrid?
Asked Answered
C

2

21

I have a class that I use in a PropertyGrid. I found that by setting CategoryAttribute on each property it creates a new category for each item, obviously. This sets my property grid to have a [+] for each item with my custom name in it, and this isn't the behavior I'm trying to achieve.

In Visual Studio, if you click on an item in the Solution Explorer, say, an assembly, it has zero tree nodes and just a list of perfectly-named properties, i.e. any string can identify a property, not just the object's name. So instead of having this:

[+ File Path]
    FilePath | propertyValue
[+ File Size]
    FileSize | 0 KB

I'm looking for this:

[+ File]
    File Path | value
    File Size | 0 KB

Or even the above without the initial [+] node. I've poured through the System.ComponentModel namespace looking for an applicable attribute but I can't find one.

How can I achieve this effect? It must be possible, Visual Studio does it and I believe they're the same component, not a derived and extended one.

Considerate answered 17/9, 2009 at 19:30 Comment(0)
L
44

Use the DisplayNameAttribute to change what text displays (make it more human readable), the DescriptionAttribute to add help text to the property, and the CategoryAttribute to group the properties..

using System.ComponentModel;

[Category("Test")]
[DisplayName("Test Property")]
[Description("This is the description that shows up")]
public string TestProperty {get;set;}
Lubalubba answered 17/9, 2009 at 19:37 Comment(2)
you can also use the Description attribute to provide a longer description to the userDissuasion
Thanks so much :) Lifesaver. +1Considerate
S
9

You'll want to have CategoryAttribute set to "File" for BOTH properties:

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

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

I recommend reading "Getting the most out of the .NET Property Grid Control" for other ideas you can use for organizing your properties, including adding descriptions.

Stoll answered 17/9, 2009 at 19:33 Comment(2)
You did miss the bit about the display name (sample in my reply if you want to add it)Lubalubba
Yeah, this solves putting them under one tree but I needed the DisplayName attribute from the selected answer. Thanks, though! :DConsiderate

© 2022 - 2024 — McMap. All rights reserved.