2013-05-21 122 views
0

組合框數據表被定義爲WPF組合框結合到不工作

<ComboBox Name="cmbCallSource" /> 

在形式負載運行此代碼(I檢查了dtSources數據表與調試器的正常內容結束):

//Init Source List 
     dtSources = new DataTable(); 
     dtSources.Columns.Add(new DataColumn("Key", Type.GetType("System.Int32"))); 
     dtSources.Columns.Add(new DataColumn("Caption", Type.GetType("System.String"))); 
     dtSources.Rows.Add(new object[] { -1, Utility.GetStringByKey("string1") }); ; 
     dtSources.Rows.Add(new object[] { 0, Utility.GetStringByKey("string2") }); ; 
     dtSources.Rows.Add(new object[] { 1, Utility.GetStringByKey("string3") }); ; 
     dtSources.Rows.Add(new object[] { 2, Utility.GetStringByKey("string4") }); ; 
     dtSources.Rows.Add(new object[] { 3, Utility.GetStringByKey("string5") }); ; 
     dtSources.Rows.Add(new object[] { 4, Utility.GetStringByKey("string6") }); ; 

     //Attempt 1 
     cmbCallSource.DataContext = dtSources.DefaultView; 
     cmbCallSource.SelectedValuePath = "Key"; 
     cmbCallSource.DisplayMemberPath = "Caption"; 

     //Attempt 2 
     //Binding myBinding = new Binding("Name"); 
     //myBinding.Source = dtSources; // data source from your example 

     //cmbCallSource.DisplayMemberPath = "Caption"; 
     //cmbCallSource.SelectedValuePath = "Key"; 
     //cmbCallSource.SetBinding(ComboBox.ItemsSourceProperty, myBinding); 

我嘗試了兩種方法(第二個被註釋掉)。 Combobox以任何一種方式結束。我在這裏錯過了什麼?

回答

0

設置的ItemSource屬性,而不是DataContext的

cmbCallSource.ItemsSource = dtSources.DefaultView; 
+0

謝謝。工作。 – user1250290