Vous devez créer un convertisseur de type, puis l'appliquer en utilisant un attribut à la classe AgeWrapper. Ensuite, la grille de propriétés utilisera ce convertisseur de type pour obtenir la chaîne à afficher. Créez un convertisseur de type comme ceci ...
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);
}
}
Notez qu'il hérite de ExpandableObjectConverter. C'est parce que la classe AgeWrapper a une propriété enfant appelée AgeWrapper.Age qui doit être exposée en ayant un bouton + à côté de l'entrée AgeWrapper dans la grille. Si votre classe n'a pas de propriétés enfants que vous souhaitez exposer, héritez à la place de TypeConverter. Maintenant, appliquez ce convertisseur à votre classe ...
[TypeConverter(typeof(AgeWrapperConverter))]
public class AgeWrapper