Search code examples
.netcollectionspropertygrid

Change property grid collection display


How can I change the (Collection) text displayed on the right column for a collection property in a property grid?

enter image description here


Solution

  • You can use a TypeConverterAttribute with a custom TypeConverter, something like this:

    public class Sample
    {
        public Sample()
        {
            Ints = new List<int>();
        }
    
        [TypeConverter(typeof(MyConverter))]
        public List<int> Ints { get; }
    }
    
    public class MyConverter : TypeConverter
    {
        public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
        {
            if (destinationType == typeof(string))
                return "Hello world";
    
            return base.ConvertTo(context, culture, value, destinationType);
        }
    }