-
Hello. I want to have an option
and I want to provide an available set of values for Any ideas are welcome. Thanks! |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 5 replies
-
Use an enum rather than a string? |
Beta Was this translation helpful? Give feedback.
-
Because the options are shown in a First, you need to change the type of the property to some type that wraps the string. In my example I called it public class RuntimeEnumProxy {
public RuntimeEnumProxy(string value) {
Value = value;
}
public string Value { get; }
public override string ToString() {
return Value;
}
} In the options type, add a [Category("My category")]
[DisplayName("Runtime Enum")]
[Description("These enum values are defined at runtime")]
[TypeConverter(typeof(RuntimeEnumTypeConverter))]
public RuntimeEnumProxy RuntimeEnum { get; set; } = new RuntimeEnumProxy(""); You'll also need to override the The public override bool GetStandardValuesSupported(ITypeDescriptorContext context) => true;
public override bool GetStandardValuesExclusive(ITypeDescriptorContext context) => true;
public override StandardValuesCollection GetStandardValues(ITypeDescriptorContext context)
{
return new StandardValuesCollection(
new[] {
new RuntimeEnumProxy("Alpha"),
new RuntimeEnumProxy("Beta"),
new RuntimeEnumProxy("Gamma"),
new RuntimeEnumProxy("Delta")
}
);
} * I'm not sure if it's actually a |
Beta Was this translation helpful? Give feedback.
Because the options are shown in a
PropertyGrid
*, which uses type descriptors and converters to get the properties to display, this can be achieved using aTypeConverter
. I've created #554 to demonstrate this. The full code is in that PR, but I'll post the relevant parts here.First, you need to change the type of the property to some type that wraps the string. In my example I called it
RuntimeEnumProxy
. It simply stores the string value.In the options type, add a
TypeConverterAtt…