组合框定义为
<ComboBox Name="cmbCallSource" />
在表单加载时,我运行此代码(我检查了 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);
我尝试了两种方法(第二种方法被注释掉了)。无论哪种方式,组合框最终都是空的。我在这里想念什么?