Что ж вы все такие непонятливые... drdoom, на самом деле в msdn по ListBox.ObjectCollection.Add не хватает пары деталей. Для item действительно вызывается ToString(), но только если класс item-а не заявляет что для его преобразования нужно использовать TypeConverter. А на List<T> кто-то поставил аттрибут c указанием CollectionConverter, который из любой строчки делает стандартный "(collection)". Тебе явно указать правильный конвертер для NamedList<T>, примерно так:
| Код | class NamedListTTypeConverter : CollectionConverter { public override bool CanConvertTo(ITypeDescriptorContext context, Type sourceType) { if (sourceType == typeof(string)) { return true; } else { return base.CanConvertFrom(context, sourceType); } }
public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType) { if (destinationType == typeof(string)) { return value.ToString(); } else { return base.ConvertTo(context, culture, value, destinationType); } } }
[TypeConverter(typeof(NamedListTTypeConverter))] class NamedList<T> : List<T> { public string Name { get; set; } public NamedList(string name) { this.Name = name; } public override string ToString() { return Name; } }
|
|