如何在 PropertyGrid 对象的集合编辑器中自定义描述? [英] How do you customize the descriptions in the Collection Editor of the PropertyGrid object?

查看:35
本文介绍了如何在 PropertyGrid 对象的集合编辑器中自定义描述?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含多个公共属性的类.这些属性之一是包含另一个类的实例的列表.它分解如下:

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

我有一个 PropertyGrid 对象它编辑次要框架对象.它有一个包含对 MinorFrameFormatDefinition 对象集合的引用的字段.当我单击此字段中的按钮打开集合编辑器,然后单击添加按钮时,我得到了:

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:

如何让集合编辑器使用 Definition 而不是 Irig106Library.Filters.PCM.MinorFrameFormatDefinition 标记对象?

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

推荐答案

你可以像这样重写 ToString()

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

或者如果你不想改变类,你也可以在上面定义一个TypeConverter:

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

这篇关于如何在 PropertyGrid 对象的集合编辑器中自定义描述?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆