This is a migrated thread and some comments may be shown as answers.

RadDataForm Spinner Enum values with multiple words

1 Answer 70 Views
DataForm
This is a migrated thread and some comments may be shown as answers.
Rodrigo
Top achievements
Rank 1
Veteran
Rodrigo asked on 21 Oct 2020, 08:41 PM

Hi,

is it possible somehow to decorate Enum values so it would display multiple words in the dropdown?

 

Something like this:

public enum MyList
{
Black,
White,
[Description("Rather Not Say")]

Unknown
}

 

Regards,

Rodrigo

1 Answer, 1 is accepted

Sort by
0
Yana
Telerik team
answered on 23 Oct 2020, 09:23 AM

Hi Rodrigo,

You can use a Converter Attribute to convert the enum type to / from a string and return the custom text, for example:

 public class MyListToStringConverter : IPropertyConverter
    {
        public object Convert(object value)
        {
            if (value != null)
            {
                switch ((MyList)value)
                {
                    case MyList.Black:
                        return "Black";
                    case MyList.White:
                        return "White";
                    case MyList.Unknown:
                        return "Rather Not Say";                 
                    default:
                        return null;
                }
            }
            else
            {
                return null;
            }
        }

        public object ConvertBack(object value)
        {
            if (value != null)
            {
                switch ((string)value)
                {
                    case "White":
                        return MyList.White;
                    case "Black":
                        return MyList.Black;
                    case "Rather Not Say":
                        return MyList.Unknown;
                  
                    default:
                        return null;
                }
            }
            else
            {
                return null;
            }
        }
    }

Then use the converter in the source class definition:

[DisplayOptions(Header = "My List")]
[Converter(typeof(MyListToStringConverter))]
public MyList MyListProperty
{
    get { return this.myListProperty; }
    set
    {
        if (value != this.myListProperty)
        {
            this.myListProperty = value;
            OnPropertyChanged();
        }
    }
}

I hope such an approach would be suitable for the scenario you have.

Regards,
Yana
Progress Telerik

Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.

Tags
DataForm
Asked by
Rodrigo
Top achievements
Rank 1
Veteran
Answers by
Yana
Telerik team
Share this question
or