Search code examples
c#.netwinformspropertygrid

Change the type name in Collection Editor in PropertyGrid


I have a property in a class ManageConfig

public class ManageConfig
{
    [Category("Config")]
    [Description("Read and write specific")]
    [DisplayName("Camera Settings")]
    public List<Config> Configuration 
    {
        get;
        set;
    }
}

public class Config
{
    public string ConfigName { get;  set; }
    public string ConfigValue { get; set; }
}

when I open the collection in the propertygrid I get window where I can add or remove Item, the Item name is the class Name Config, I want the item name changes when I change the ConfigName property for each item.

Thanks


Solution

  • You can override ToString method of Config class to specify what the collection editor show in left panel in list of items:

    public override string ToString()
    {
        if(!string.IsNullOrEmpty(ConfigName))
            return ConfigName;
        return base.ToString();
    }