c# - 如何使用枚举填充组合框 - 在组合框中显示枚举的整数(值)

标签 c# winforms combobox enums

这是我的枚举:

    Name = Value
public enum BitRates
{
    bitrate_32 = 32,
    bitrate_40 = 40,
    bitrate_48 = 48,
    bitrate_56 = 56,
    bitrate_64 = 64,
    bitrate_80 = 80,
    bitrate_96 = 96,
    bitrate_112 = 112,
    bitrate_128 = 128,
    bitrate_160 = 160,
    bitrate_192 = 192,
    bitrate_224 = 224,
    bitrate_256 = 256,
    bitrate_320 = 320
}

这是我的代码:

    cb_birates.DataSource = Enum.GetValues(typeof(BitRates));
    cb_birates.SelectedItem = BitRates.bitrate_128;

但是在这些代码之后,我的组合框中有名称(例如 bitrate_128)。
我想在组合框中显示值(例如 128)。
我怎样才能做到这一点?

最佳答案

将它们转化为潜在的值(value)观:

cb_birates.DataSource = Enum.GetValues(typeof(BitRates))
                            .Cast<BitRates>()
                            .Select(x => (int)x)
                            .ToList();

关于c# - 如何使用枚举填充组合框 - 在组合框中显示枚举的整数(值),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30385296/

相关文章:

c# - 尽管有 SelectedItem 和 ItemsSource 绑定(bind),组合框仍未更新

c# - ASP.NET 核心 : How to pass connection string to DBContext?

linux - Windows 应用程序使用 SharpSVN - 需要将服务器迁移到 linux SVN 服务器 - 它仍然有效吗?

c# - 如何在MessageBox中显示textBox控件?

c# - 如何在 C# 中将窗体居中

combobox - PropertyRef 需要 ComboBox

c# - 如何在 C# 中等待 WebBrowser 控件

c# - 在 .NET 中从内存下载并运行 JAR

c# - 如何获得 HLSL 计算的输出?

c# - 消除组合框中的空条目