我有一个ComboBox事件,“SelectionChange”。获取选定索引的文本WPF组合框
这里就是我想要做的事:
- 我只要有两个组合框
- 第二个组合框将显示根据所选择的项目上的第一个框项目
- ComboBox2应反应在ComboBox1上的一个项目被选中
我的问题是当我试图获取SelectedIndex。
当确认SelectedIndex后使用ComboBox1.Text时,它将返回null,因此ComboBox2不会作出反应。
我试着放置一个按钮来强制该事件,它确实工作。似乎SelectedIndex只有在您释放焦点后才会更改。
这里的代码片段:
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吗?另外,你的代码是什么事件处理程序? – 2012-02-26 21:01:03
它是Box的SelectionChanged事件的一部分 – Nath 2012-02-26 21:05:17