2012-02-26 80 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也可以用來獲取以前選定的項目。