I have a dude, i want to select a value from wpf Combobox
I have the next code:
private void CargarUnidades()
{
List<Unidades> unidades;
using (var sesion = NHibernateFluentConfiguracion.AbrirSesion())
{
using (var transaccion = sesion.BeginTransaction())
{
unidades = Unidades.ObtenerTodaslasUnidades();
}
}
cmbUnidad.ItemsSource = unidades;
cmbUnidad.DisplayMemberPath = "Nombre";
cmbUnidad.SelectedValuePath = "Nombre";
}
After I charge the unidades
CargarUnidades(); //Charge all unidades in the combobox
Articulos c = Articulos.Obtener(id_articulo);
//Get Articulo from the database for the id
//In the last query I get the unidad same that exists in cmbUnidad
//previusly charge
//I assing the value but in the combobox doesnt appear selected,
//appear nothing
cmbUnidad.SelectedValue = c.id_unidad;
txtCodigo.Text = c.Codigo;
.
.
.
.
How I can select a value from combobox???
Note: I m new in WPF and my english is not good je je je
Thanks for the answer Firo, I modify the code and this is the result
cmbUnidad.Items.Cast<Unidades>().FirstOrDefault(u => u.id_unidad == c.id_unidad.id_unidad);
I dont know if this is the best way but its functionaly 😛
selectedValue must euqal (using Equals) with one of the values in the list hence assign the object not the id because the control does not know how to select based on the id
UPDATE: since the
Articuloshas only the id of unidades and not a reference (which i would have mapped) you have to search for it.