0

I'm working with small wpf app and I need to load enumation into my combo box as possiblities that user might choose when he's adding new product and it looks like this:

my enumeration:

public enum ProductTypeEnum : int
{
    Unknown = 0,

    [StringValue("Final product for sale")]
    FinalProduct = 1,

    [StringValue("Ingredient for product")]
    Material = 2

}

Here is how it looks in my C# WPF App when I'm fetching values to display it in my combobox:

var myEnums = ProductTypeEnum.GetValues(typeof(ProductTypeEnum));


if(myEnums.Length > 0)
{
    cmbArticleType.ItemsSource = myEnums;
  //cmbArticleType.DisplayMemberPath = "SOMETHING.. WHATEVER IT AINT WORKED";

}

enter image description here

And this displays as you can see my real values "FinalProduct" and not the "Final product for sale"

[StringValue("Final product for sale")]

and I would like to display Final product for sale because it looks more natural in combobox (with spaces and so) =)

Thanks guys for any kind of help Cheers

EDIT AFTER MM8 HELPS:

How could I get real value out of this, cast it in integer as enums holds value of 0,1,2,3 etc :

enter image description here

enter image description here

But articleType does not provide Value so I could cast it in integer and store to db?

Roxy'Pro
  • 4,216
  • 9
  • 40
  • 102
  • @Clemens I can not see how this is duplicate as I'm not using same things like in that link you provided, there is Descritpion propert and I'm using StringValue which is type of System.Attribute and which should hold description for each enum – Roxy'Pro Dec 17 '18 at 13:59
  • 1
    You can't simply *use* Description? – Clemens Dec 17 '18 at 13:59
  • @Clemens I can't it gaves me an error, and in link you gave me there is no written, if that Description exist, which namespace 'owns' it, what do I need to include? – Roxy'Pro Dec 17 '18 at 14:00
  • @Clemens is it part of System.ComponentModel.Description or whatever? – Roxy'Pro Dec 17 '18 at 14:02
  • Perhaps helpful and nearly a duplicate https://stackoverflow.com/questions/3985876/wpf-binding-a-listbox-to-an-enum-displaying-the-description-attribute – kenny Dec 17 '18 at 15:51

1 Answers1

1

Given the following method that extracts the value of the attribute:

public static string GetStringValue(Enum value)
{
    Type type = value.GetType();
    FieldInfo fieldInfo = type.GetField(value.ToString());
    StringValueAttribute[] attribs = fieldInfo.GetCustomAttributes(
        typeof(StringValueAttribute), false) as StringValueAttribute[];
    return attribs.Length > 0 ? attribs[0].StringValue : null;
}

...you can create instances of anonymous that has a Description property:

cmbArticleType.ItemsSource = myEnums.OfType<ProductTypeEnum>().Select(x => new { Value = x, Description = GetStringValue(x) });
cmbArticleType.DisplayMemberPath = "Description";
cmbArticleType.SelectedValuePath = "Value";

You get the currently selected enum value from the SelectedValue property of the ComboBox:

ProductTypeEnum selectedValue = (ProductTypeEnum)cmbArticleType.SelectedValue;
mm8
  • 163,881
  • 10
  • 57
  • 88
  • You included it in your sample code...It's a custom attribute: https://weblogs.asp.net/stefansedich/enum-with-string-values-in-c. But you might as well use the built-in Description attribute: https://www.codementor.io/cerkit/giving-an-enum-a-string-value-using-the-description-attribute-6b4fwdle0 – mm8 Dec 17 '18 at 14:43
  • Set the `SelectedValuePath` property to "Value" and cast the `SelectedValue` property of the `ComboBox`. See my edit. – mm8 Dec 17 '18 at 15:33