Wpf 键值对组合框。保存在数据库中后的文本

Wpf 键值对组合框。保存在数据库中后的文本,wpf,entity-framework-6,Wpf,Entity Framework 6,我处理两个组合框,其代码如下: var Masters = new List<KeyValuePair<long, string>>(); using (var ctx = new GestioneCaricoEntities()) { var comti = ctx.ViewMasters.OrderBy(m => m.Lastname).ToList(); foreach (var t in comti) { Master

我处理两个组合框,其代码如下:

var Masters = new List<KeyValuePair<long, string>>();
using (var ctx = new GestioneCaricoEntities())
{
    var comti = ctx.ViewMasters.OrderBy(m => m.Lastname).ToList();
    foreach (var t in comti)
    {
        Masters.Add(new KeyValuePair<long, string>(t.Id, t.Lastname));
    }
}
cmbComteCaricazione.ItemsSource = Masters;
问题:一切正常,db中表格的字段有idComandanteCaricazione,但当我再次加载窗口时,我希望看到CMBCOMTECCARICAZIONE.text中的值。
如何操作?

您需要从数据库中加载
idcomandanteaciazione
,并将
组合框的
SelectedValue
属性设置为:

public MainWindow()
{
    InitializeComponent();
    this.Loaded += (s, e) =>
    {
        using (var ctx = new GestioneCaricoEntities())
        {
            ...
            cmbComteCaricazione.ItemsSource = Masters;
            cmbComteCaricazione.SelectedValue = ts.IdComandanteCaricazione;
        }

    };
}
ts.IdComandanteCaricazione = (long?)cmbComteCaricazione.SelectedValue;
public MainWindow()
{
    InitializeComponent();
    this.Loaded += (s, e) =>
    {
        using (var ctx = new GestioneCaricoEntities())
        {
            ...
            cmbComteCaricazione.ItemsSource = Masters;
            cmbComteCaricazione.SelectedValue = ts.IdComandanteCaricazione;
        }

    };
}