2012-02-26 38 views
2

我有一个ComboBox事件,“SelectionChange”。获取选定索引的文本WPF组合框

这里就是我想要做的事:

  1. 我只要有两个组合框
  2. 第二个组合框将显示根据所选择的项目上的第一个框项目
  3. 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" /> 
+0

你能发布相关的XAML吗?另外,你的代码是什么事件处理程序? – 2012-02-26 21:01:03

+0

它是Box的SelectionChanged事件的一部分 – Nath 2012-02-26 21:05:17

回答

3

为了快速成功,您可以访问ComboBox1.SelectedValue或ComboBox1.SelectedItem而不是ComboBox1.Text。

您的主要问题似乎是在ComboBox1中的选择更改时,它不会直接更改ComboBox1.Text,您(即焦点)将不得不离开ComboBox1直到更新文本。通常,您可以通过使用数据绑定来避免此类问题,而不是使用基于事件的方法。

+0

哇!这是正确的在我面前,我从来没有想过使用它。谢谢!我已经编程了将近一天,我对使用数据绑定的担心是我需要从头开始。谢谢! – Nath 2012-02-26 21:32:03

1

它似乎并不像使用绑定?我会建议使用绑定,然后将绑定的UpdateSourceTrigger属性更改为UpdateSourceTrigger.PropertyChanged

在底层对象中,你可以监听一个propertychanged事件,但一定要实现INotifyPropertyChanged。

举一个例子来看看http://www.tanguay.info/web/index.php?pg=codeExamples&id=304

在一些详细信息:

在视图确保ü设置的DataContext和填充一年收集 此外impliment的INotifyPropertyChanged的

对于一个组合框此另一方面它几乎是一样的。

private ObservableCollection<KeyValuePair<string, string>> _yearValues = new ObservableCollection<KeyValuePair<string, string>>(); 
    public ObservableCollection<KeyValuePair<string, string>> YearValues 
    { 
     get 
     { 
      return _yearValues; 
     } 

     set 
     { 
      _yearDownValues = value; 
      OnPropertyChanged("YearValues"); 
     } 
    } 

    private string _selectedYear; 
    public string SelectedYear 
    { 
     get 
     { 
      return _selectedYear; 
     } 

     set 
     { 
      _selectedYear = value; 
      OnPropertyChanged("SelectedYear"); 
     } 
    } 

一定要钩OnPropertyChanged,并做你的事

public event PropertyChangedEventHandler PropertyChanged; 
    protected void OnPropertyChanged(string propertyName) 
    { 
     if (propertyName == "SelectedYear") 
     { 
      // populate subj collection which will update the combobox 
     } 
    } 

在您的XAML:

<ComboBox Name="YearCombobox" 
     ItemsSource="{Binding YearValues}" 
     SelectedValue="{Binding SelectedYear}" 
     SelectedValuePath="Key" 
     DisplayMemberPath="Value"/> 
<ComboBox Name="SubjCombobox" 
     ItemsSource="{Binding SubjValues}" 
     SelectedValue="{Binding SelectedSubj}" 
     SelectedValuePath="Key" 
     DisplayMemberPath="Value"/> 
+0

您可以发布关于您的评论的样本或代码段代码吗?我只需要看看它是如何完成的。谢谢 – Nath 2012-02-26 21:08:22

+0

哦,因为它是一个ObservableCollection,你不需要我相信的UpdateSourceTrigger。 ObservableCollections非常强大,但大多数需要一个CollectionView – rfcdejong 2012-02-26 21:20:38

+0

谢谢,先生。 – Nath 2012-02-26 21:35:41

0

你尝试

e.AddedItems[0] 

看来,如果你是不使用MVVM(你应该通用y)有时候SelectionChanged会变为null。选择这个而不是其他人。

尝试

var selectionItem = e.AddedItems[0] as ComboBoxItem; 
string text = selectionItem.Content.ToString(); 

的e.RemovedItems也可以用来获取以前选定的项目。