3

I have a class that contains several public properties. One of those properties is a List containing instances of another class. It breaks down something like this:

namespace Irig106Library.Filters.PCM
{
    [Description("Definition")]
    public class MinorFrameFormatDefinition
    {
        [Description("Word Number")]
        public int WordNumber { get; set; }

        [Description("Number of Bits")]
        public int NumberOfBits { get; set; }
    }

    public class MinorFrame
    {
        // ... other properties here

        [Category("Format")]
        [Description("Minor Frame Format Definitions")]
        public List<MinorFrameFormatDefinition> MinorFrameFormatDefinitions { get; set; }
    }
}

I have a PropertyGrid object which edits the Minor Frame object. It has a field containing a reference to the collection of MinorFrameFormatDefinition objects. When I click on the button in this field to open the Collection Editor, and click the Add button, I get this:

enter image description here

How do I get the collection editor to label the objects with Definition instead of Irig106Library.Filters.PCM.MinorFrameFormatDefinition?

Robert Harvey
  • 178,213
  • 47
  • 333
  • 501

1 Answers1

4

You could override ToString(), like this

public class MinorFrameFormatDefinition
{
    [Description("Word Number")]
    public int WordNumber { get; set; }

    [Description("Number of Bits")]
    public int NumberOfBits { get; set; }

    public override string ToString()
    {
        return "hello world";
    }
}

Or if you don't want to change the class, you can also define a TypeConverter on it:

[TypeConverter(typeof(MyTypeConverter))]
public class MinorFrameFormatDefinition
{
    [Description("Word Number")]
    public int WordNumber { get; set; }

    [Description("Number of Bits")]
    public int NumberOfBits { get; set; }
}

public class MyTypeConverter : 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);
    }
}
Simon Mourier
  • 132,049
  • 21
  • 248
  • 298
  • It works, but how can I change the header of the Collection Editor? For example change "MinorFrameFormatDefinition Collection Editor" to "My Frame Collection Editor". – Andark Jan 27 '15 at 09:03
  • @Andark - this is unrelated - ask another question – Simon Mourier Jan 27 '15 at 11:12