2012-02-26 24 views
2

ComboBoxのイベント「SelectionChange」があります。選択したインデックスのテキストを取得するWPF ComboBox

ここで私がやろうとしているものです:

  1. 私は2番目コンボボックスがComboBox2はできるだけ早く反応すべき
  2. 最初のボックスで選択した項目に応じたアイテムが表示されます2つのコンボボックス
  3. を持っていますComboBox1の項目が選択されています

私の問題は、SelectedIndexを取得しようとしているときです。

SelectedIndexを確認した後でComboBox1.Textを使用すると、ComboBox2が反応しないようにnullが返されます。

イベントを強制するためにボタンを配置しようとしましたが、動作しました。フォーカスを解放するまで、SelectedIndexは変更されないようです。バインディング使用のよう

<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を投稿できますか?また、あなたのコード部分はどのイベントハンドラですか? –

+0

これは、ボックスのSelectionChangedイベントの一部です – Nath

答えて

3

すぐに成功するには、ComboBox1.Textの代わりにComboBox1.SelectedValueまたはComboBox1.SelectedItemにアクセスします。

あなたの主な問題は、ComboBox1の選択が変更されたときにComboBox1.Textを直接変更しないようです。つまり、フォーカス(つまりフォーカス)は、テキストが更新されるまでComboBox1を離れる必要があります。通常、このような問題を回避するには、このイベントベースのアプローチの代わりにデータバインディングを使用します。

+0

うわー!それは私の目の前にあって、私はそれを使うことは考えなかった。ありがとうございました!私はほぼ一日のプログラミングをしてきました。データバインディングの使用に関する私の心配は、私が最初から始めなければならないということでした。ありがとうございました! – Nath

1

それはいないようです。ここでは、2つのCBのXAMLだ

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; 
} 

は、ここでは、コードのスニペットですか?バインディングを使用して、UpdateSourceTrigger.PropertyChangedにバインディングのUpdateSourceTriggerプロパティを設定することをお勧めします。

下位オブジェクトでは、uはpropertychangedイベントをリッスンできますが、必ずINotifyPropertyChangedを実装してください。例えば

は、もう少し詳細にhttp://www.tanguay.info/web/index.php?pg=codeExamples&id=304

を見て:

視Uは、1つのコンボボックスこのためにINotifyPropertyChangedの

をimplimentのDataContextを設定し、年間のコレクション を移入してくださいもう一方はほぼ同じです。

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をフックして、XAMLであなたのもの

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

を行うようにしてください:あなたがある場合

<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

+0

ああ、それはObservableCollectionだから、私は信じているUpdateSourceTriggerは必要ありません。 ObservableCollectionsは本当に強いですが、ほとんどCollectionViewが必要です – rfcdejong

+0

ありがとうございます。 – Nath

0

あなたは

e.AddedItems[0] 

を試してみましたそれはそうですMVVMを使っていないy)SelectionChangedはnullになることがあります。他人の代わりにこれを選んでください。

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

e.RemovedItemsも、以前に選択した項目を取得するために使用することができます試してみてください。

関連する問題