Do an IsSelected in a ComboBox with the SelectedValue [UWP]

0

I have a ComboBox, which already has some assigned items, 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 shows me the DataAnotation of the name, so all right, but in the database when I consult the People table there is a field called "TipoDoc" that by requirements is a bit field, for when loading TipoDoc brings me a number from 1 to 9 no more, but I need to load a user the comboBox I load the type of document you have.

If I do it, for example with a Combo_Tipo.SelectedIndex = 3; the comboBox will select the third one in the "Passport" list, but I need the ComboBox to be selected with the value, instead of using the SelectedIndex use the SelectedValue. That to put it for example Combo_Tipo.SelectedValue = "Nit"; I leave selected the type of document NIT.

But in doing so he does not 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 data of the Combo 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 charge it in the codeBehind of the XAML window osea TerceroPage.xaml.cs

this.Combo_TipoDoc.ItemsSource = Enum.GetValues(typeof(TERCERO_TIPOID)).Cast<TERCERO_TIPOID>();
    
asked by Wilmilcard 04.10.2018 в 00:51
source

1 answer

0

As you indicate in your comment what you need is to make a cast of int to Enum and vice versa, here is an example:

 public enum TERCERO_TIPOID : byte
    {
        [Display(Name = "Nit")]
        NIT,//0
        [Display(Name = "Cedula Cuidadania")]
        CC,//1
        [Display(Name = "Cedula Extranjeria")]
        CE,//2
        [Display(Name = "Pasaporte")]
        PASAPORTE,//3
        [Display(Name = "Tarjeta Extranjeria")]
        TAR_EXT,//4
        [Display(Name = "Doc. Id Extranjeria")]
        DOCID_EXT,//5
        [Display(Name = "Tarjeta Identidad")]
        TAR_IDEN,//6
        [Display(Name = "Nuip")]
        NUIP,//7
        [Display(Name = "Codigo")]
        COD,//8
        [Display(Name = "Otros")]
        OTRO = 9
    }

since the datasource that you indicate in your question is a collection of enums of TERCERO_TIPOID :

Combo_TipoDoc.ItemsSource = Enum.GetValues(typeof(TERCERO_TIPOID)).Cast<TERCERO_TIPOID>();

How you want to get it is the value entero or byte of your enum you can do it this way getting it from the SelectedItem :

var  document = (TERCERO_TIPOID)Combo_TipoDoc.SelectedItem;
int idDocumento = (int)document;

And to convert from entero to Enum :

var valorEntero=3;
TERCERO_TIPOID dipoDoc = (TERCERO_TIPOID)valorEntero;
    
answered by 11.10.2018 / 00:52
source