2017-04-10 40 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