型コンバーターを作成し、属性を使用してそれを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