C# 将枚举值添加到一个简单的组合框

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15040872/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 13:51:46  来源:igfitidea点击:

adding enum values to a simple combobox

c#wpfcomboboxenums

提问by user2061405

i have a really simple question to ask about C# and WPF. My Question will follow after this attempt of mine:

关于 C# 和 WPF,我有一个非常简单的问题要问。在我的这次尝试之后,我的问题将随之而来:

private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            foreach (var item in Races)
            {
                cbRace.Items.Add(item);
            }
        }
    }

    enum Races
    {
        Human=1,
        Dwarf,
        Elf,
        Orc,
        Goblin,
        Vampire,
        Centaur
    }

Ok so, my question is how will I add the values(e.g. Human,dwarf,elf....) into the combo box: cbRace? sorry I'm new to C# so i would rally appreciate it if someone can help me out :), thanks in advance.

好的,我的问题是如何将值(例如 Human、dwarf、elf....)添加到组合框中:cbRace?抱歉,我是 C# 的新手,所以如果有人可以帮助我,我将不胜感激:),提前致谢。

采纳答案by user2061405

private void Window_Loaded(object sender, RoutedEventArgs e)
    {
        foreach (var item in Enum.GetValues(typeof(Races)))
        {
            cbRace.Items.Add(item);
        }
    }
    enum Races
    {
        Human = 1,
        Dwarf,
        Elf,
        Orc,
        Goblin,
        Vampire,
        Centaur
    }

回答by Crwydryn

You should be able to do something like this:

你应该能够做这样的事情:

cbRace.DataSource = Enum.GetValues(typeof(Races));

Checkout thisanswer for more information on setting and retrieving the enum values.

查看答案以获取有关设置和检索枚举值的更多信息。

回答by Faaiz Khan

use this

用这个

cbRace.Datasource = Enum.GetValues(typeof(Races));

to databind your enum to the combobox and then use selectedValue and selectedText properties of your combobox to retreive names and values;

将枚举数据绑定到组合框,然后使用组合框的 selectedValue 和 selectedText 属性来检索名称和值;

回答by Clemens

This would perhaps be the easiest way to set the ComboBox items:

这可能是设置 ComboBox 项的最简单方法:

private void Window_Loaded(object sender, RoutedEventArgs e)
{
    cbRace.ItemsSource = Enum.GetValues(typeof(Races));
    cbRace.SelectedIndex = 0;
}

It is not necessary to loop over the enum values, just set the ItemsSourceproperty.

不需要遍历枚举值,只需设置ItemsSource属性即可。

回答by MikeT

This isn't a preferred solution as Clemens has already given you that but if you wanted to add in the XAML directly you could also do

这不是首选解决方案,因为 Clemens 已经为您提供了该解决方案,但如果您想直接添加 XAML,您也可以这样做

<ComboBox>
    <urCode:Races>Human</urCode:Races>
    <urCode:Races>Dwarf</urCode:Races>
    <urCode:Races>Elf</urCode:Races>
</ComboBox>

you could also implment an IValueConverter that when bound to a Type, returns the Enum.GetValues

你也可以实现一个 IValueConverter,当绑定到一个类型时,返回 Enum.GetValues

回答by VIKINX

cmbUserType.Items.AddRange(core.Global.ToObjectArray(Enum.GetValues(typeof(STATUS))));
public enum STATUS { INACTIVE, ACTIVE }

回答by VIKINX

Shortest Way to add Enum Values to Combobox in C#

在 C# 中将枚举值添加到 Combobox 的最短方法

class User{

public enum TYPE { EMPLOYEE, DOCTOR, ADMIN };

}

// Add this class to your form load event of Form Cunstructor

// 将此类添加到 Form Cunstructor 的表单加载事件中

cmbUserType.Items.AddRange(Enum.GetNames(typeof(User.TYPE)));