Search code examples
c#collectionsgraphicspropertygrid

How can I delete the default display string "(Collection)" in a PropertyGrid?


my goal is to replace the display value of a PropertyGrid property trough an own UITypeEditor. But I'm not able to delete the default display string "(Collection)" that is always shown.

I want to remove the "(Collection)" string

I tried e.Graphics.Clear and drawing with a white brush into the graphics. But that's not working. Here's my code:

public class MyUITypeEditor : UITypeEditor
{
    public override void PaintValue(PaintValueEventArgs e)
    {
        // Not working:
        //e.Graphics.Clear(Color.White);
        //using (SolidBrush brush = new SolidBrush(Color.White))
        //{
        //    e.Graphics.FillRectangle(brush, e.Bounds);
        //}

        e.Graphics.DrawString(
            "0|0, 10|10",
            new Font("Arial", 10f, FontStyle.Bold),
            new SolidBrush(Color.Black),
            new Point(0, 0));
    }

    public override bool GetPaintValueSupported(ITypeDescriptorContext context)
    {
        return true;
    }
}

Solution

  • What you have to do is to define a new TypeConverter element and override the methods below:

    public class test_typeconverter : TypeConverter
    {
        public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture,
        object value, Type destinationType)
                => "Text requiered";
    
        public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType) => true;
    
        public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType) => false;
    }
    

    Then, you have to define this type as the type converter of the collection you want to show in the property grid as below:

    [TypeConverter(typeof(test_typeconverter))]
    public List<int> Values { get; set; }
    

    Result of the code