您需要创建一个类型转换器,然后将该属性应用于AgeWrapper类。然后,属性网格将使用该类型转换器来获取要显示的字符串。创建一个像这样的类型转换器...
public class AgeWrapperConverter : ExpandableObjectConverter
{
public override bool CanConvertTo(ITypeDescriptorContext context,
Type destinationType)
{
// Can always convert to a string representation
if (destinationType == typeof(string))
return true;
// Let base class do standard processing
return base.CanConvertTo(context, destinationType);
}
public override object ConvertTo(ITypeDescriptorContext context,
System.Globalization.CultureInfo culture,
object value,
Type destinationType)
{
// Can always convert to a string representation
if (destinationType == typeof(string))
{
AgeWrapper wrapper = (AgeWrapper)value;
return "Age is " + wrapper.Age.ToString();
}
// Let base class attempt other conversions
return base.ConvertTo(context, culture, value, destinationType);
}
}
请注意,它从ExpandableObjectConverter继承。这是因为AgeWrapper类有一个名为AgeWrapper.Age的子属性,需要通过在网格中的AgeWrapper条目旁边有一个+按钮来公开。如果您的类没有您想要公开的任何子属性,那么应该从TypeConverter继承。现在将此转换器应用于您的课程...
[TypeConverter(typeof(AgeWrapperConverter))]
public class AgeWrapper