If my array property has null items, then I can't open CollectionEditor from PropertyGrid. I get error with text 'component'. How can I fix it?
public partial class Form1 : Form
{
public Test[] test { get; set; }
public Form1()
{
InitializeComponent();
test = new Test[5];
test[2] = new Test() { Name = "2" };
propertyGrid1.SelectedObject = this;
}
}
[TypeConverter(typeof(ExpandableObjectConverter))]
public class Test
{
public string Name { get; set; }
}
Maybe I should override some methods in my custom CollectionEditor, but i don't know which
CodePudding user response:
It depends exactly how you want to work around this, but if you just need to exclude the null values, then you can override the default ArrayEditor class, something like this;
// define the editor on the property
[Editor(typeof(MyArrayEditor), typeof(UITypeEditor))]
public Test[] test { get; set; }
...
public class MyArrayEditor : ArrayEditor
{
public MyArrayEditor(Type type) : base(type)
{
}
protected override object[] GetItems(object editValue)
{
// filter on the objects you want the array editor to use
return base.GetItems(editValue).Where(i => i != null).ToArray();
}
}