0

In my project I need to bing the following enum

public enum eFlex_IO_COMP_CM
{
    ePan = 0,
    eComp = 1,
}

into a combobox. I've a public list, valorized as follows:

public IEnumerable<eFlex_IO_COMP_CM> EnumFlexIoCompCm { get; set; }

EnumFlexIoCompCm = Enum.GetValues(typeof(eFlex_IO_COMP_CM)).Cast<eFlex_IO_COMP_CM>();

This is the XAML code:

<ComboBox 
    Grid.Column="20" 
    Width="50" 
    ItemsSource="{Binding Path=EnumFlexIoCompCm, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=local:ViewerFlexConfig}}" 
    SelectionChanged="Combo_SelectionChanged" 
    DisplayMemberPath="Value" 
    SelectedValuePath="Key" 
    SelectedValue="{Binding ConfigObject.COMP_CounterMode, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
    />

Basically I want to display the name of enum's item and set the key as value because I need to store in the database the int and not the description. The problem is that I cannot even display the name of enum's item and set correctly the value, as If I try to save, the value from that combobox is name. Any helps?

  • 1
    this helped me : https://stackoverflow.com/questions/6145888/how-to-bind-an-enum-to-a-combobox-control-in-wpf – user3041165 Oct 21 '19 at 12:50
  • 2
    Note that `IEnumerable` is not a Dictionary. The elements do not have a `Key` and a `Value` property. Consider changing the type of the EnumFlexIoCompCm to `Dictionary`. – Clemens Oct 21 '19 at 12:52
  • And of course if the integer values are consecutive, starting at 0, you may simply bind the ItemsSource to a collection of strings or enum values, and then bind the SelectedIndex property. – Clemens Oct 21 '19 at 13:01
  • Remove `DisplayMemberPath="Value" and SelectedValuePath="Key"`. Then you should see the "name" of the enumeration values in the `ComboBox`. `ConfigObject.COMP_CounterMode` should be an `eFlex_IO_COMP_CM` property that you can simply cast to an `int` whenever you need: `int val = (int)eFlex_IO_COMP_CM;` – mm8 Oct 21 '19 at 13:13
  • Perfect @Clemens, I converted my IEnumerable into Dictionary and all works fine, thanks! –  Oct 21 '19 at 13:16

0 Answers0