是的,您可以specify an System.ComponentModel.Editor attribute on your list of strings, with StringCollectionEditor as the editor。您需要在项目中添加对 System.Design.Dll 的引用,以便编译。
例如,假设你的对象是这样的:
[DefaultProperty("Name")]
public class CustomObject
{
[Description("Name of the thing")]
public String Name { get; set; }
[Description("Whether activated or not")]
public bool Activated { get; set; }
[Description("Rank of the thing")]
public int Rank { get; set; }
[Description("whether to persist the settings...")]
public bool Ephemeral { get; set; }
[Description("extra free-form attributes on this thing.")]
[Editor(@"System.Windows.Forms.Design.StringCollectionEditor," +
"System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a",
typeof(System.Drawing.Design.UITypeEditor))]
[TypeConverter(typeof(CsvConverter))]
public List<String> ExtraStuff
{
get
{
if (_attributes == null)
_attributes = new List<String>();
return _attributes;
}
}
private List<String> _attributes;
}
它的属性网格如下所示:
点击...你会得到:
如果您不喜欢内置的收藏编辑器,you can implement your own custom collection editor。
我的示例显示了 TypeConverter 属性的使用。如果您不这样做,则列表在道具网格中显示为“(集合)”。类型转换器gets it to display as something intelligent。例如,要在属性网格中显示集合的短字符串表示形式,如下所示:
...TypeConverter 是这样的:
public class CsvConverter : TypeConverter
{
// Overrides the ConvertTo method of TypeConverter.
public override object ConvertTo(ITypeDescriptorContext context,
CultureInfo culture, object value, Type destinationType)
{
List<String> v = value as List<String>;
if (destinationType == typeof(string))
{
return String.Join(",", v.ToArray());
}
return base.ConvertTo(context, culture, value, destinationType);
}
}
List<String> 上不需要设置器,因为集合编辑器不设置该属性,它只是添加或删除该属性的条目。所以只需提供吸气剂。