Вам необходимо, как вы упомянули, TypeConverter
и переопределить следующие методы (подробности см. В прикрепленной ссылке):
// Override the two methods below when you need new instance per user edit
public override bool GetCreateInstanceSupported(ITypeDescriptorContext context)
{
return true;
}
public override object CreateInstance(ITypeDescriptorContext context,
IDictionary propertyValues)
{
return new EditorConfiguration
{
Value1 = (string) propertyValues["Value1"],
Value2 = (string) propertyValues["Value2"]
};
}
// You get the two methods below if inherit from ExpandableObjectConverter
// If you need extra logic such as property name ordering, you still need to
// override the correct methods to do so
public override bool GetPropertiesSupported(ITypeDescriptorContext context)
{
return true;
}
public override PropertyDescriptorCollection GetProperties(
ITypeDescriptorContext context, object value,
Attribute[] attributes)
{
return TypeDescriptor.GetProperties(typeof(EditorConfiguration), attributes);
}
И использовать преобразователь типов:
[TypeConverter(typeof(EditorConfigurationConverter))]
public class EditorConfiguration
{
public string Value1 { get; set; }
public string Value2 { get; set; }
}
Тогда в конструкторе вы можете увидеть:
Если все свойства можно настроить через конструктор, вам может не потребоваться пользовательский UITypeEditor
.
UITypeEditor
требуется вам только тогда, когда вы хотите предоставить альтернативный / расширенный опыт для настройки конфигурации. Например, если вы хотите прочитать конфигурацию из файла, вы можете реализовать UITypeEditor
, чтобы иметь возможность выбирать файл, используя диалоговое окно «Открыть файл» / селектор файлов.