2017-04-20 114 views
0

我有2个组合框。当我选择/更改第一个组合框中的项目时,第二个组合框将启用。但第二个组合框总是显示我以前的项目。组合框SelectionChanged事件表明以前的项目

例如: 更改项目在组合框1和ComboBox2不显示我的任何项目。 我再次在组合框1中更改项目。现在ComboBox2显示我应该通过第一次更改显示的项目。

这里是我的代码:

private void categoryComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e) 
{ 
    subCategoryComboBox.Items.Clear(); 
    subCategoryComboBox.IsEnabled = true; 

    string SelectedCategoryID = ConvertBackCategory(categoryComboBox.Text); 
    connection = new MySqlConnection(conf.connection_string); 
    if (this.OpenConnection() == true) 
    { 
     try 
     { 
      using (MySqlCommand cmd = new MySqlCommand()) 
      { 
       cmd.Connection = connection; 
       cmd.CommandText = "SELECT name FROM auftrags_typ_childcategory WHERE category = @CategoryID"; 
       cmd.Parameters.AddWithValue("@CategoryID", SelectedCategoryID); 
       using (MySqlDataReader reader = cmd.ExecuteReader()) 
       { 
        while (reader.Read()) 
        { 
         subCategoryComboBox.Items.Add(reader["name"].ToString()); 
        } 
       } 
      } 

     } 
     catch (MySqlException ex) 
     { 
      MessageBox.Show(ex.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error); 
     } 
    } 
} 

我怎样才能解决这个问题? /为什么它显示我不是目前的项目?

回答

2

The Text property of the WPF ComboBox不更新,直到SelectionChanged事件引发后。不要问我为什么,但事实并非如此。他们似乎已将Text属性仅用于具有可编辑文本的组合框。

SelectionChanged升起之前ComboBox的每一个其他相关属性更新

SelectedItem,SelectedValueSelectedIndex将在你的SelectionChanged处理程序中都是正确和当前的。 I answered this question yesterday;你应该能够适应你的需求而不会有太多麻烦,但是让我知道你是否遇到了麻烦。

+0

哦,谢谢。我没有在谷歌中看到这个问题,所以我虽然在这里不存在这种类型的失败.. –