在这里,我的程序显示了一个表单,您可以在其中输入有关某个主题的详细信息和该特定主题的副主题。
private void OnLoad(object sender, RoutedEventArgs e)
{
string connectionString = String.Format("Data Source={0}", _db);
string queryString1 = "SELECT TopicSubject FROM tblTopic ORDER BY TopicNumber";
// Populates subject combo box without duplicates
SQLiteDataAdapter da = new SQLiteDataAdapter(queryString1, connectionString);
DataTable dt = new DataTable();
da.Fill(dt);
for (int i = 0; i < dt.Rows.Count; i++)
{
if (cmbSubject.Items.Contains(dt.Rows[i]["TopicSubject"]))
{ }
else
{
cmbSubject.Items.Add(dt.Rows[i]["TopicSubject"]);
}
}
PopulateSubtopicCombobox();
}
private void PopulateSubtopicCombobox()
{
string connectionString = String.Format("Data Source={0}", _db);
string queryString2 = "SELECT * FROM tblTopic ORDER BY TopicNumber";
// Populates subtopic combo box without duplicates and relating to subject
SQLiteDataAdapter da2 = new SQLiteDataAdapter(queryString2, connectionString);
DataTable dt2 = new DataTable();
da2.Fill(dt2);
for (int i = 0; i < dt2.Rows.Count; i++)
{
if (cmbSubtopic.Items.Contains(dt2.Rows[i]["TopicDescription"]))
{ }
else
{
// If no subject has been chosen (also when first loaded)
if (cmbSubject.Text == "")
{
cmbSubtopic.Items.Add(dt2.Rows[i]["TopicDescription"]);
}
// If a subject has been chosen, only display subtopics related to that subject ***BROKEN***
else if (cmbSubject.Text == Convert.ToString(dt2.Rows[i]["TopicSubject"]))
{
cmbSubtopic.Items.Add(dt2.Rows[i]["TopicDescription"]);
}
}
}
}
// When subject combo box is changed, refresh the subtopic combo box
private void cmbSubject_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
cmbSubtopic.Items.Clear();
PopulateSubtopicCombobox();
}
这样做的目的是让cmbSubtope只显示共享主题的副主题(因此您不能为数学主题选择历史副主题)。
但是,SelectionChanged事件似乎过早地运行了PopolateSubtopicCombobox()。当您第一次选择一个主题时,cmbSubtopy值不会改变。当您选择第二个主题时,cmb主题将更改为仅显示先前所选主题的副主题。
有什么帮助吗?
1条答案
按热度按时间gajydyqb1#
我已经重现了这个问题
问题是cmbSubject.Text尚未更新
使用SelectionChangedEventArgs e而不是cmbSubject.Text
MainWindow.xmal