获取所选索引WPF ComboBox的文本

时间:2012-02-26 20:40:42

标签: c# wpf

我有一个ComboBox的事件,“SelectionChange”。

这是我正在尝试做的事情:

  1. 我有两个ComboBoxes
  2. 第二个ComboBox将根据第一个Box上所选项目显示项目
  3. ComboBox2应在选择ComboBox1上的项目后立即作出反应
  4. 我的问题是当我想要获取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" />
    

3 个答案:

答案 0 :(得分:3)

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

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

答案 1 :(得分:1)

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

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

有关示例,请查看http://www.tanguay.info/web/index.php?pg=codeExamples&id=304

更详细一点:

在视图中确保您设置DataContext并填充年份集合 还要实现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"/>

答案 2 :(得分:0)

你试过吗

e.AddedItems[0]

看起来如果你没有使用MVVM(通常你应该这样做),有时SelectionChanged会变为null。选择此而不是其他。

尝试

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

e.RemovedItems也可用于获取先前选择的项目。