错误的列表,没有在组合框中绑定

时间:2012-07-10 12:04:59

标签: c# wpf

  

可能重复:
  Not binding path in datagrid (wpf)

数据网格中的组合框!的 ahtung!

 <ComboBox Name="mex" DataContext="{Binding RelativeSource={RelativeSource Self}}" Style="{DynamicResource ComboBoxStyle}"  ItemsSource="{Binding Path=combolist}" SelectionChanged="status_SelectionChanged" Height="Auto" Width="Auto">
</ComboBox>

所以

MySqlCommand status_db = new MySqlCommand("select name_ru from request_status", conn);
MySqlDataReader combodata = status_db.ExecuteReader();
List<string> combolist = new List<string>();
  while (combodata.Read())
  {
     combolist.Add(combodata.GetString(0));
  }

为什么组合框中的项目为空?我疯了!!!

1 个答案:

答案 0 :(得分:1)

您正在使用RelativeSource Self作为DataContext,这意味着DataContext将是ComboBox本身。绑定ItemsSource时,它将尝试查找名为combolist的ComboBox的属性(当然不存在)。

要解决此问题,您可以

  • 将ComboBox的DataContext更改为您的控件(或窗口,页面,...),并使列表成为控件的属性。
  • 直接在后面的代码中设置ItemsSource:mex.ItemsSource = combolist
相关问题