How do you customize the descriptions in the Collection Editor of the PropertyGrid object?

最后都变了- 提交于 2019-12-13 15:05:34

问题


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:

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


回答1:


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);
    }
}


来源:https://stackoverflow.com/questions/15800029/how-do-you-customize-the-descriptions-in-the-collection-editor-of-the-propertygr

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!