4

In C#, when using a PropertyGrid where an object has a Collection, what determines if the value next to the DisplayName shows the value of "(Collection)"?

Is there a specific attribute for this value?

Thanks

Simon
  • 7,991
  • 21
  • 83
  • 163
  • It seems `(Collection)` appears on a `List` and not on `IEnumerable`. A fixed array has an expandable node which is quite different from a collection. – John Alexiou Sep 02 '15 at 14:35

1 Answers1

3

You can use TypeConverters.

public class MyCollectionTypeConverter : TypeConverter
{
    public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
    {
        if (value is List<string>)
        {
            return string.Join(",", ((List<string>) value).Select(x => x));
        }
        return base.ConvertTo(context, culture, value, destinationType);
    }
}

and add as attribute;

    [TypeConverter(typeof(MyCollectionTypeConverter))]
    public List<string> Prop1 { get; set; }

Ref: How to: Implement a Type Converter

kaya
  • 724
  • 10
  • 24