I have a ComboBox, which already has some items assigned, loaded by means of an enumeration (Enum):
public enum TERCERO_TIPOID : byte {
[Display(Name = "Nit")]NIT,
[Display(Name = "Cedula Cuidadania")]CC,
[Display(Name = "Cedula Extranjeria")]CE,
[Display(Name = "Pasaporte")]PASAPORTE,
[Display(Name = "Tarjeta Extranjeria")]TAR_EXT,
[Display(Name = "Doc. Id Extranjeria")]DOCID_EXT,
[Display(Name = "Tarjeta Identidad")]TAR_IDEN,
[Display(Name = "Nuip")]NUIP,
[Display(Name = "Codigo")]COD,
[Display(Name = "Otros")]OTRO = 9 }
In the comboBox it already shows me the DataAnotation of the name, so far so good, but in the database when I query the People table there is a field called "TipoDoc" which by requirements is a bit field, therefore when loading TipoDoc brings me a number from 1 to 9 no more, but I need the comboBox to load the type of document it has when loading a user.
If I do it, for example with a Combo_Tipo.SelectedIndex = 3;
the comboBox will select the third in the "Passport" enumeration, but I need the ComboBox to be selected with the value, instead of using the SelectedIndex use the SelectedValue. That when giving it, for example Combo_Tipo.SelectedValue = "Nit";
, I leave the type of NIT document selected.
But when I do it, he doesn't let me do it. How do I do it? What am I doing wrong?
The SelectedIndex captures me and assigns me the value in the combo.
The SelectedValue only captures the Combo data but does not assign it to me when I need it.
Combo:
<ComboBox Header="Tipo Documento:" Width="150" HorizontalAlignment="Center" VerticalAlignment="Top" x:Name="Combo_TipoDoc" SelectedItem="{Binding Tipoid, Mode=TwoWay}" />
and this is the Object in the field in the VM:
public TERCERO_TIPOID Tipoid { get => tipoid; set { tipoid = value; RaisePropertyChanged(); } }
private TERCERO_TIPOID tipoid;
and so I load it in the codeBehind of the XAML window, that isTerceroPage.xaml.cs
this.Combo_TipoDoc.ItemsSource = Enum.GetValues(typeof(TERCERO_TIPOID)).Cast<TERCERO_TIPOID>();
As you indicate in your comment, what you need is to cast from
int
toEnum
and vice versa, here is an example:since the datasource that you indicate in your question is a collection of the enums of
TERCERO_TIPOID
:Since you want to get the value
entero
obyte
from your enum you can do it this way by getting it from theSelectedItem
:And to convert from
entero
toEnum
: