将 SelectedValuePath 辅助到 Comobox 后,分配时仍采用 SelectedValue null

本文关键字:null SelectedValue 分配 SelectedValuePath Comobox | 更新日期: 2023-09-27 17:56:01

将 SelectedValuePath 同温化到 Comobox 后,分配时仍然采用 SelectedValue null。组合框填充数据,但当 id 分配给 SelectedValue 时,它始终返回 null。

C#:

var fillDoctor = (from a in db.Persons
                           select new
                                  {
                                      id = a.id,
                                      name = a.name + " " + a.middle_name + " " + a.last_name
                                  });
                _cmbDoctor.ItemsSource = fillDoctor;
                _cmbDoctor.DisplayMemberPath = "name";//"first_name" + " " + "middle_name" + " " + "last_name";
                _cmbDoctor.SelectedValuePath = "id";
                _cmbDoctor.SelectedValue = 51;
MessageBox.Show(_cmbDoctor.SelectedValue.ToString());

将 SelectedValuePath 辅助到 Comobox 后,分配时仍采用 SelectedValue null

你确定你有一个ID为51的人吗?如果没有这样的 ID,则组合框没有任何可指向的内容