簡體   English   中英

獲取選定索引WPF ComboBox的文本

[英]Getting the text of selected index WPF ComboBox

我有一個組合框“ SelectionChange”事件。

這是我想做的事情:

  1. 我有兩個組合框
  2. 第二個ComboBox將根據第一個Box上的選定項目顯示項目
  3. 一旦選擇了ComboBox1上的一項,ComboBox2應該做出反應

我的問題是當我嘗試獲取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" />

為了獲得快速成功,您可以訪問ComboBox1.SelectedValue或ComboBox1.SelectedItem而不是ComboBox1.Text。

您的主要問題似乎是,當ComboBox1中的選擇更改時,它不會直接更改ComboBox1.Text,您(即焦點)將不得不離開ComboBox1,直到更新Text。 通常,您可以通過使用數據綁定而不是基於事件的方法來避免此類問題。

看起來好像不是二手書? 我建議使用綁定,然后將綁定的UpdateSourceTrigger屬性更改為UpdateSourceTrigger.PropertyChanged

然后,您可以在底層對象中偵聽propertychanged事件,但請確保實現INotifyPropertyChanged。

有關示例, 請參見http://www.tanguay.info/web/index.php?pg=codeExamples&id=304

更詳細一點:

在視圖中,確保您設置了DataContext並填充了Year集合還隱含了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"/>

你試過了嗎

e.AddedItems[0]

看來,如果您不使用MVVM(通常應該這樣做),則SelectionChanged確實會變為null。 選擇此而不是其他。

嘗試

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

e.RemovedItems也可以用於獲取先前選擇的項目。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM