.net 如何将ComboBox选定值检索为枚举类型?

.net 如何将ComboBox选定值检索为枚举类型?,.net,wpf,enums,.net,Wpf,Enums,这是我的Enum代码: public enum EmployeeType { Manager, Worker } 我将初始化组合框值,同时窗体将自身初始化为 combobox1.ItemsSource = Enum.GetValues(typeof(EmployeeType)); 现在,我想以EmployeeTypeenum的形式检索ComboBox的选定值,而不是以字符串或整数等形式检索 非常感谢您的帮助。您只需要做相反的事情,对于给定的字符串,正确的类型是什么 Empl

这是我的
Enum
代码:

public enum EmployeeType
{
    Manager,
    Worker
}
我将初始化
组合框
值,同时窗体将自身初始化为

 combobox1.ItemsSource = Enum.GetValues(typeof(EmployeeType));
现在,我想以
EmployeeType
enum的形式检索
ComboBox
的选定值,而不是以字符串或整数等形式检索


非常感谢您的帮助。

您只需要做相反的事情,对于给定的字符串正确的类型是什么

EmployeeType result = 
          (EmployeeType)Enum.Parse(typeof(EmployeeType), 
          combobox1.SelectedItem.Content);
p.S.我不知道
combobox1.SelectedItem.Content
是否正确,我不太喜欢WCF

EmployeeType selected = (EmployeeType)combobox1.SelectedItem;
不过,您可能需要先检查null(无选择)

为了完整起见,这里是我设置的示例程序。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>

以及背后的代码:

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());
            }
        }
    }
}
使用系统;
使用System.Windows;
命名空间WpfApplication1
{
/// 
///MainWindow.xaml的交互逻辑
/// 
公共部分类主窗口:窗口
{
公共主窗口()
{
初始化组件();
_ComboBox.ItemsSource=Enum.GetValues(typeof(无论什么));
}
公共枚举
{
一
二
三
四
}
私有无效按钮\u单击(对象发送者,路由目标e)
{
if(_ComboBox.SelectedItem==null)
{
MessageBox.Show(“无选择”);
}
其他的
{
Whatever=(Whatever)\u ComboBox.SelectedItem;
Show(whatever.ToString());
}
}
}
}
尝试以下方法: 首先将组合框指定给GetNames方法,这将使用枚举的名称而不是数字填充项。 然后处理更改的事件并将字符串解析回枚举

    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();
    }

要添加的一件事是,您可以将数据注释显示属性添加到每个枚举中,并编写一个方法来解析枚举并显示属性的name属性,而不是枚举,从而使其更易于使用。

实际上,如果我必须这样做,我会在绑定之前构建一个
ObservableCollection
(例如,在视图模型的构造函数中,使用
Enum.GetNames(typeof(EmployeeType))
然后迭代每个值以将其重新分析为
EmployeeType
类型

设置好集合后,您只需将其绑定到您的
组合框
,然后,在选择项目时,您应该检索类型
EmployeeType
,而无需解析它

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; }
    }
}
所选对象应返回类型为
EmployeeType
的对象


PS:关于代码拼写错误,很抱歉,我现在附近没有任何C编辑器;)

不会的。我在发布之前设置了一个测试项目进行确认。
 <ComboBox ItemsSource="{Binding EmployeeTypes}" />