【发布时间】:2012-02-26 20:40:41
【问题描述】:
我有一个 ComboBox 事件“SelectionChange”。
这是我想要做的:
- 我有两个组合框
- 第二个 ComboBox 将根据第一个 Box 上的所选项目显示项目
- ComboBox2 应在选择 ComboBox1 上的项目后立即做出反应
我的问题是当我尝试获取 SelectedIndex 时。
当我在确认 SelectedIndex 后使用 ComboBox1.Text 时,它返回 null,因此 ComboBox2 没有反应。
我尝试放置一个按钮来强制事件,它确实有效。在您释放焦点之前,SelectedIndex 似乎不会改变。
这是代码的sn-p:
if (cb_subj.SelectedIndex == ctr)
{
cb_section.Items.Clear();
if (connectToDB.openConnection() == true)
{
MySqlDataAdapter comboBoxItems_seclist = new MySqlDataAdapter();
MySqlCommand query = new MySqlCommand(@"SELECT section_code FROM sections
WHERE subject = @subj", connectToDB.connection);
query.Parameters.AddWithValue("@subj", cb_subj.Text);
comboBoxItems_seclist.SelectCommand = query;
System.Data.DataTable classlist = new System.Data.DataTable();
comboBoxItems_seclist.Fill(classlist);
foreach (System.Data.DataRow row in classlist.Rows)
{
string rows = string.Format("{0}", row.ItemArray[0]);
cb_section.Items.Add(rows);
}
}
break;
}
这是两个 CB 的 XAML:
<ComboBox Height="23" HorizontalAlignment="Left" Margin="166,12,0,0" Name="cbox_year" VerticalAlignment="Top" Width="120" SelectionChanged="cbox_year_SelectionChanged">
<ComboBoxItem Content="1st Year / 1st Sem" />
<ComboBoxItem Content="1st Year / 2nd Sem" />
<ComboBoxItem Content="2nd Year / 1st Sem" />
<ComboBoxItem Content="2nd Year / 2nd Sem" />
<ComboBoxItem Content="3rd Year / 1st Sem" />
<ComboBoxItem Content="3rd Year / 2nd Sem" />
<ComboBoxItem Content="4th Year / 1st Sem" />
<ComboBoxItem Content="4th Year / 2nd Sem" />
</ComboBox>
<ComboBox Height="23" HorizontalAlignment="Left" Margin="166,41,0,0" Name="cb_subj" VerticalAlignment="Top" Width="120" SelectionChanged="cb_subj_SelectionChanged" />
【问题讨论】:
-
你能发布相关的 XAML 吗?另外,您的代码属于哪个事件处理程序?
-
这是 Boxes 的 SelectionChanged 事件的一部分