WPF: How to populate combobox with enum in Xaml

前端 未结 2 2002
名媛妹妹
名媛妹妹 2021-01-07 10:19

I know there are several ways to do it, but I would like to make it even easier if possible because I have a lot of comboboxes to bind in this way. There is a suggestion usi

相关标签:
2条回答
  • 2021-01-07 10:26

    You would be better off with a MarkupExtension, like this one.

    0 讨论(0)
  • 2021-01-07 10:43

    CodeNaked posts a great way of doing this

    For your approach to work you can change the converter to Enum.GetValues(value as Type) and use the x:Type syntax as Source for the Binding

    ItemsSource="{Binding Source={x:Type local:MyValues},
                          Converter={StaticResource EnumToArrayConverter}}"
    

    EnumToArrayConverter

    public class EnumToArrayConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return Enum.GetValues(value as Type);
        }
        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return null; // I don't care about this
        }
    }
    
    0 讨论(0)
提交回复
热议问题