I've checked the answers of this question: Modifying structure property in a PropertyGrid
And also SizeConverter
from .net.
But not helpful, my property is still not saved.
I have a struct, a user control, and a custom type converter.
public partial class UserControl1 : UserControl
{
public Bar bar { get; set; } = new Bar();
}
[TypeConverter(typeof(BarConverter))]
public struct Bar
{
public string Text { get; set; }
}
public class BarConverter : ExpandableObjectConverter
{
public override bool GetCreateInstanceSupported(ITypeDescriptorContext context)
{
return true;
}
public override object CreateInstance(ITypeDescriptorContext context, IDictionary propertyValues)
{
if (propertyValues != null && propertyValues.Contains("Text"))
return new Bar { Text = (string)propertyValues["Text"] };
return new Bar();
}
}
After compile, I drag the control in a form then I can see the property Bar.Text
showed in the properties window, I also can edit the value and it seems be saved.
But nothing is generated in the InitializeComponent
method
So if I reopen the designer, the Text field in the properties window become empty.
Please notice the struct hasn't a custom constructor, so I cannot use InstanceDescriptor
.
Do I miss any important steps?