locked
Display Enum Values for Windows Store App

    Question

  • I'm trying to display the values of an enum, so the user can select one of the values. I've tried the code below, but nothing appears. If there's a better control to use, I'm open to suggestions. The enum contains 6 values, and the user needs to select one to complete a task. Any ideas?

    <ComboBox x:Name="cboTypes" Margin="20" ItemsSource="{Binding MyContainerClass, Mode=OneWay}" SelectedValue="{Binding MyEnumProperty, Mode=TwoWay,
    Converter={StaticResource enumToStringConverter}}">


    
    

    Randy

    Friday, March 07, 2014 8:34 PM

Answers

  • I ended up creating an ObservableCollection<string> property, then loaded the property by looping through the enum:

    foreach(MyEnum myEnum in Enum.GetValues(typeof(MyEnum)))
    {
        _myEnumCollection.Add(myEnum.ToString());
    }

    I was able to bind MyEnumCollection to the ItemsSource of the ComboBox in the XAML code. I still have the SelectedValue of the ComboBox set to MyType, Mode=TwoWay, and the converter, which appears below:

    public sealed class EnumToStringConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, string language)
        {
            string enumString;
            try
            {
                enumString = Enum.GetName((value.GetType()), value);
                return enumString;
            }
            catch
            {
                return string.Empty;
            }
        }
    
    
        public object ConvertBack(object value, Type targetType, object parameter, string language)
        {
            throw new NotImplementedException();
        }
    }
    Cheers!


    Randy

    Monday, March 10, 2014 3:13 PM

All replies

  • I believe this sample from the Dev Center will be helpful.

    Localizing Enums (in a ComboBox)

    Friday, March 07, 2014 9:00 PM
  • I appreciate the response, but I not find this useful. The tcd:EnumNameConverter isn't findable in the source code. The SetupItems is an ExtensionMethod - I cannot find the body of that method either. The code does not compile out of the box.

    I'd still appreciate a pointer to a working example of populating a combobox from an enum in a Windows 8.1 Store App.

    Thanks...


    Randy

    Monday, March 10, 2014 1:00 PM
  • I ended up creating an ObservableCollection<string> property, then loaded the property by looping through the enum:

    foreach(MyEnum myEnum in Enum.GetValues(typeof(MyEnum)))
    {
        _myEnumCollection.Add(myEnum.ToString());
    }

    I was able to bind MyEnumCollection to the ItemsSource of the ComboBox in the XAML code. I still have the SelectedValue of the ComboBox set to MyType, Mode=TwoWay, and the converter, which appears below:

    public sealed class EnumToStringConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, string language)
        {
            string enumString;
            try
            {
                enumString = Enum.GetName((value.GetType()), value);
                return enumString;
            }
            catch
            {
                return string.Empty;
            }
        }
    
    
        public object ConvertBack(object value, Type targetType, object parameter, string language)
        {
            throw new NotImplementedException();
        }
    }
    Cheers!


    Randy

    Monday, March 10, 2014 3:13 PM