CatBytesAndCoffee
Topic Author
Posts: 12
Joined: 23 Mar 2024, 18:08

System.ComponentModel.EnumConverter

28 Aug 2024, 18:06

I attempted to use a System.ComponentModel.EnumConverter as described by https://brianlagunas.com/a-better-way-t ... ms-in-wpf/. It works great in Blend but it does not seem to be recognized when running in Unity. (The code doesn't appear to execute and I tried to put a breakpoint there but it wasn't recognized as a valid location.) Should I be able to use a converter like this?
 
User avatar
sfernandez
Site Admin
Posts: 3154
Joined: 22 Dec 2011, 19:20

Re: System.ComponentModel.EnumConverter

02 Sep 2024, 19:02

You should be able to define a TypeConverter for your type the way it is explained in that article
[TypeConverter(typeof(EnumDescriptionTypeConverter))]
public enum Status
Although we don't expose the EnumConverter class (could you please create a ticket for this?), I think you won't need it in this case, you can just return the string values you want from the enum value received (or using the Description attribute approach to make it more generic).
    public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
    {
        if (destinationType == typeof(string) && value is MyEnumType)
        {
            MyEnumType enumValue = (MyEnumType)value;
            switch (enumValue)
            {
                case Option1: return "This is the first option";
                case Option2: return "This is the second option";
                ...
            }
        }

        return string.Empty;
    }
 
CatBytesAndCoffee
Topic Author
Posts: 12
Joined: 23 Mar 2024, 18:08

Re: System.ComponentModel.EnumConverter

02 Sep 2024, 19:21

Thanks for the suggestion! I'll try it that way, and will raise that ticket on the EnumConverter class.

Ticket submitted: https://www.noesisengine.com/bugs/view.php?id=3679
 
User avatar
sfernandez
Site Admin
Posts: 3154
Joined: 22 Dec 2011, 19:20

Re: System.ComponentModel.EnumConverter

10 Sep 2024, 11:03

Thanks for the ticket

Who is online

Users browsing this forum: Bing [Bot] and 3 guests