RadDataForm Spinner Enum values with multiple words

2 posts, 0 answers
  1. Rodrigo
    Rodrigo avatar
    13 posts
    Member since:
    May 2018

    Posted 21 Oct Link to this post

    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

  2. Yana
    Admin
    Yana avatar
    5002 posts

    Posted 23 Oct Link to this post

    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/.

Back to Top