Вопрос

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?

Это было полезно?

Решение

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);
    }
}
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top