【问题标题】:Getting the text of selected index WPF ComboBox获取所选索引 WPF ComboBox 的文本
【发布时间】:2012-02-26 20:40:41
【问题描述】:

我有一个 ComboBox 事件“SelectionChange”。

这是我想要做的:

  1. 我有两个组合框
  2. 第二个 ComboBox 将根据第一个 Box 上的所选项目显示项目
  3. ComboBox2 应在选择 ComboBox1 上的项目后立即做出反应

我的问题是当我尝试获取 SelectedIndex 时。

当我在确认 SelectedIndex 后使用 ComboBox1.Text 时,它返回 null,因此 ComboBox2 没有反应。

我尝试放置一个按钮来强制事件,它确实有效。在您释放焦点之前,SelectedIndex 似乎不会改变。

这是代码的sn-p:

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" />

【问题讨论】:

  • 你能发布相关的 XAML 吗?另外,您的代码属于哪个事件处理程序?
  • 这是 Boxes 的 SelectionChanged 事件的一部分

标签: c# wpf


【解决方案1】:

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

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

【讨论】:

  • 哇!它就在我面前,我从没想过要使用它。谢谢!我已经编程了将近一天,我对使用数据绑定的担忧是我需要从头开始。谢谢!
【解决方案2】:

看起来不像是用过的绑定?我建议使用绑定,然后将绑定的 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"/>

【讨论】:

  • 您能否发布有关您的评论的示例或 sn-p 代码?我只需要看看它是如何完成的模式。谢谢
  • 哦,因为它是 ObservableCollection,所以我相信你不需要 UpdateSourceTrigger。 ObservableCollections 非常强大,但主要需要 CollectionView
【解决方案3】:

你试过了吗

e.AddedItems[0]

如果您不使用 MVVM(通常应该这样做),SelectionChanged 似乎有时会变为 null。选择这个而不是其他的。

试试

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-03-08
    • 1970-01-01
    • 1970-01-01
    • 2011-11-12
    • 2021-12-22
    • 1970-01-01
    • 2019-07-17
    相关资源
    最近更新 更多