Converter to show description of an enum, and convert back to enum value on selecting an item from combo box in wpf

后端 未结 5 1193
耶瑟儿~
耶瑟儿~ 2020-12-15 08:13

I am using an enum to enlist values in my combobox. I want to write a converter that would show the "description" of the selected enum value. And, when selected, i

5条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-15 08:31

    I know this is an old question, but for some reason, this is rather complicated, even though it seems like it'd be a pretty common task (I'm currently doing this on a UWP app). Using a combination of the accepted answer, some other items I found, and a bit of my own work, here's the simplest way I've found to accomplish this menial task. In short:

    • Define your enum along w/ setting the description in the Display attribute
    • Create a converter that converts from an enum value to the description
    • In your viewmodel, expose a collection of enum values from which to choose, the selected enum value, then initialize those
    • Define a couple of handy enum extension methods
    • Finally, some simple binding to the ComboBox, just overriding its ItemTemplate to use the converter.

    Enum

    public enum EnumOptions
    {
        [Display(Description = "Option 1")]
        OptionOne= 1,
        [Display(Description = "Option 2")]
        OptionTwo,
        [Display(Description = "Option 3")]
        OptionThree
    }
    

    Converter

    public class EnumToDisplayConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, string language)
        {
            var enumValue = value as Enum;
    
            return enumValue == null ? DependencyProperty.UnsetValue : enumValue.GetDescriptionFromEnumValue();
        }
    
        public object ConvertBack(object value, Type targetType, object parameter, string language)
        {
            return value;
        }
    }
    

    Viewmodel (partial)

    public IReadOnlyList Options { get; }
    
    private EnumOptions _selectedOption;
    
    public EnumOptions SelectedOption
    {
        get { return _selectedOption; }
        set
        {
            _selectedOption = value;
            OnPropertyChanged(() => SelectedOption);
        }
    }
    
    // Initialization in constructor
    Options = EnumExtensions.GetValues().ToArray();
    // If you want to set a default.
    SelectedOption = Options[0];
    

    Extensions

    public static class EnumExtensions
    {
        public static string GetDescriptionFromEnumValue(this Enum value)
        {
            var attribute = value.GetType()
                .GetField(value.ToString())
                .GetCustomAttributes(typeof(DisplayAttribute), false)
                .SingleOrDefault() as DisplayAttribute;
            return attribute == null ? value.ToString() : attribute.Description;
        }
    
        /// 
        /// Enumerates all enum values
        /// 
        /// Enum type
        /// IEnumerable containing all enum values
        /// 
        public static IEnumerable GetValues()
        {
            return Enum.GetValues(typeof (T)).Cast();
        }
    }
    

    XAML (partial)

    Choose an option
    
        
            
                
            
        
    
    

提交回复
热议问题