2017-04-10 37 views
1

我处理两个组合框丝毫遵循代码:密钥值对组合框的.text后保存在数据库

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; 

<ComboBox x:Name="cmbComteCaricazione" Margin="0,5,0,0" 
      DisplayMemberPath="Value" SelectedValuePath="Key" Width="110" IsEditable="True" /> 

我节省数据库:

ts.IdComandanteCaricazione = (long?)cmbComteCaricazione.SelectedValue; 

问题:一切去正确的方式,在db中的表的字段有idComandanteCaricazione但是当我再次加载窗口我喜欢请参阅cmbComteCaricazione.text中的值。 我该怎么做?

回答

1

您需要加载从数据库中idComandanteCaricazioneComboBoxSelectedValue属性设置为它:

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

    }; 
} 
+0

完美!非常感谢你 ! – Disaji