SilverLight
SilverLight

Reputation: 20468

How fill combobox using with enum - show integers(values) of enum in combobox

Here is my enum :

    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
}

And here is my codes :

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

But after these codes i have Names(e.g. bitrate_128) in my combobox.
I want to show Values(e.g. 128) in my combobox.
How can i do that?

Upvotes: 2

Views: 302

Answers (2)

Fabio
Fabio

Reputation: 32445

I know that answer is already accepted
But want to share a approach without casting enum type to integer:

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

Upvotes: 0

Grant Winney
Grant Winney

Reputation: 66449

Cast them to their underlying values:

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

Upvotes: 2

Related Questions