我有个家伙,我想从wpf Combobox中选择一个值
我有下一个代码:
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";
}
我向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;
.
.
.
.
如何从组合框中选择一个值??? 注意:我是WPF的新人,我的英语不是很好的je je je
感谢Firo的回答,我修改了代码,这就是结果
cmbUnidad.Items.Cast<Unidades>().FirstOrDefault(u => u.id_unidad == c.id_unidad.id_unidad);
我不知道这是不是最好的方式,但它的功能是:P
答案 0 :(得分:0)
selectedValue必须euqal(使用Equals)与列表中的一个值因此分配对象而不是id,因为控件不知道如何根据id选择
cmbUnidad.SelectedValue = c;
更新:由于Articulos
只有id的id而不是引用(我会映射),你必须搜索它。
cmbUnidad.SelectedValue = cmbUnidad.Items.Cast<Unidades>().FirstOrDefault(u => u.Nombre == c.id_unidad);