Reputation: 8349
In C#, when using a PropertyGrid
where an object has a Collection
, what determines if the value next to the DisplayName
shows the value of "(Collection)"
?
Is there a specific attribute for this value?
Thanks
Upvotes: 5
Views: 1703
Reputation: 1389
Kaya's answer didn't work for me - but - I found when you have any type converter, the property grid will show base.ToString() instead of (Collection), we can use that.
public class CollectionToStringOverrideConverter : TypeConverter
{
}
[TypeConverter(typeof(CollectionToStringOverrideConverter))]
public class FormattedList<T> : List<T>
{
public override string ToString()
{
if (this?.Count > 0)
return string.Join("|", this);
return "No items in collection";
}
}
The FormattedList class now has a type converter, so the default ToString() is used, and we can put whatever we want there. Just define your property as a FormattedList instead of a List and you're set.
Upvotes: 0
Reputation: 754
You can use TypeConverters.
public class MyCollectionTypeConverter : TypeConverter
{
public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
{
if (value is List<string>)
{
return string.Join(",", ((List<string>) value).Select(x => x));
}
return base.ConvertTo(context, culture, value, destinationType);
}
}
and add as attribute;
[TypeConverter(typeof(MyCollectionTypeConverter))]
public List<string> Prop1 { get; set; }
Ref: How to: Implement a Type Converter
Upvotes: 4