How to retrieve ComboBox selected value as enum type?

后端 未结 4 1788
情书的邮戳
情书的邮戳 2021-01-17 01:30

This is my Enum code:

public enum EmployeeType
{
    Manager,
    Worker
}

I will initialize ComboBox values whil

相关标签:
4条回答
  • 2021-01-17 01:55
    EmployeeType selected = (EmployeeType)combobox1.SelectedItem;
    

    You might want to check for null (no selection) first though.

    For completeness, here was the sample program I set up. The XAML:

    <Window x:Class="WpfApplication1.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            Title="MainWindow" Height="350" Width="525">
        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition />
                <RowDefinition />
            </Grid.RowDefinitions>
            <ComboBox x:Name="_ComboBox" />
            <Button Grid.Row="1" Click="Button_Click" />
        </Grid>
    </Window>
    

    and the code-behind:

    using System;
    using System.Windows;
    
    namespace WpfApplication1
    {
        /// <summary>
        /// Interaction logic for MainWindow.xaml
        /// </summary>
        public partial class MainWindow : Window
        {
            public MainWindow()
            {
                InitializeComponent();
                _ComboBox.ItemsSource = Enum.GetValues(typeof(Whatever));
            }
    
            public enum Whatever
            {
                One,
                Two,
                Three,
                Four
            }
    
            private void Button_Click(object sender, RoutedEventArgs e)
            {
                if (_ComboBox.SelectedItem == null)
                {
                    MessageBox.Show("No Selection");
                }
                else
                {
                    Whatever whatever = (Whatever)_ComboBox.SelectedItem;
                    MessageBox.Show(whatever.ToString());
                }
            }
        }
    }
    
    0 讨论(0)
  • 2021-01-17 01:56

    Actually, if I would had to do that, I would have built an ObservableCollection<EmployeeType> previously to the binding (for exemple in the constructor of your view model, using the Enum.GetNames(typeof(EmployeeType)) and then iterating on each values to reparse them as EmployeeType types.

    Once you have your collection set up, you just have to bind it to your ComboBox and then, when selecting an item, you should retreive a type EmployeeType without having to parse it.

    public class VieModel
    {
        private ObservableCollection<EmployeeType> _internal;
        public ViewModel()
        {
           _internal = new ObservableCollection<EmployeeType>();
           var tempList =  Enum.GetNames(typeof(EmployeeType));
    
           foreach(var val in tempList)
           {
               EmployeeType et = Enum.Parse(typeof(EmployeeType),val);
               internal.Add(et);
           }
        }
    
        public ObservableCollection<EmployeeType> EmployeeTypes
        {
           get { return _internal; }
        }
    }
    

    Then set the view model as the data context of your view, and bind your combobox to EmployeeTypes:

     <ComboBox ItemsSource="{Binding EmployeeTypes}" />
    

    The selected should return an object of type EmployeeType.

    PS: Sorry about code spelling mistakes, I don't have any C# editor near me right now ;)

    0 讨论(0)
  • 2021-01-17 02:04

    Try something like this: First assign the combo box to the GetNames method, this will populate the items with the names of the enum rather than the numbers. Then handle the changed event and parse the string back to an enum.

        public enum EmployeeType
        {
            Manaer,
            Worker
        }
    
        public MainWindow()
        {
            InitializeComponent();
            this.combobox1.ItemsSource = Enum.GetNames(typeof(EmployeeType));
            this.combobox1.SelectionChanged += new SelectionChangedEventHandler(combobox1_SelectionChanged);
        }
    
        void combobox1_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            EmployeeType selection = (EmployeeType)Enum.Parse(typeof(EmployeeType), (string)this.combobox1.SelectedItem);
            this.ResultsTextBlock.Text = selection.ToString();
        }
    

    One thing to add is, you can add the data annotation Display attribute to each enum and write a method that will parse the enum and display the attribute's name property rather than the enum making it more user friendly.

    0 讨论(0)
  • 2021-01-17 02:13

    You just need to do the reverse, for a given string, what's the correct type.

    EmployeeType result = 
              (EmployeeType)Enum.Parse(typeof(EmployeeType), 
              combobox1.SelectedItem.Content);
    

    P.S. I dont know if combobox1.SelectedItem.Content is correct, I don't do that much WCF.

    0 讨论(0)
提交回复
热议问题