簡體   English   中英

Combobox.Text總是返回Null

[英]Combobox.Text always returning Null

我正在嘗試從組合框中獲取當前選擇的選項,並且嘗試通過它獲取文本

ComboBox.Text

Combobox.SelectedItem()

但是.Text返回一個空字符串,而SelectedItem()返回null

這是有關如何填充組合框的代碼。 組合框的值取決於另一個組合框的值。

private void cboSite_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
        BackgroundWorker bw = new BackgroundWorker();
        cboPlan.Items.Clear();
        bw.DoWork += new DoWorkEventHandler(bw_cboPlan);
        bw.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bw_cboPlanComplete);
        site = cboSite.SelectedItem.ToString();
       Busy.IsBusy = true;
        Busy.BusyContent = "Loading Products";
        bw.RunWorkerAsync();
    }

    void bw_cboPlan(object sender, DoWorkEventArgs e)
    {
        SqlConnection con = new SqlConnection(Class.GetConnectionString());
        SqlCommand scProduct = new SqlCommand("spSelectProduct", con);
        scProduct.Parameters.Add(new SqlParameter("@Site",site));
        scProduct.CommandType = CommandType.StoredProcedure;

        SqlDataReader readerPortal;
        con.Open();

        readerPortal = scProduct.ExecuteReader();

        while (readerPortal.Read())
        {
            this.Dispatcher.Invoke((Action)delegate(){cboPlan.Items.Add(readerPortal[0]);});
        }
        con.Close();
    }

    void bw_cboPlanComplete(object sender, RunWorkerCompletedEventArgs e)
    {
        cboPlan.SelectedIndex = 0;
        Busy.IsBusy = false;
    }

盡管我可以在組合框中看到.Text值,但無法在代碼中使用它們。

編輯:Null值由cboPlan組合cboPlan返回。

這是當SelectedItem()返回null, .Text返回空字符串時

if (IsValid())
        {
            BackgroundWorker bw = new BackgroundWorker();
            cboPlan.Items.Clear();
            bw.DoWork += new DoWorkEventHandler(bw_Add);
            bw.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bw_AddComplete);
            plan = cboPlan.Text;
            Busy.IsBusy = true;
            Busy.BusyContent = "Sending Request.";
            bw.RunWorkerAsync();
        }

組合框的XAML。

<ComboBox x:Name="cboSite" HorizontalAlignment="Left" Margin="461,52,0,0" VerticalAlignment="Top" Width="174" SelectionChanged="cboSite_SelectionChanged"/>
<ComboBox x:Name="cboPlan" HorizontalAlignment="Left" Margin="395,106,0,0" VerticalAlignment="Top" Width="240" />

編輯

好吧,您的代碼中包含以下內容:

  BackgroundWorker bw = new BackgroundWorker();
  // You will delete all items here!
  cboPlan.Items.Clear();
  bw.DoWork += new DoWorkEventHandler(bw_Add);
  bw.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bw_AddComplete);
  // plan is String.Empty because there are no items in the combobox!
  plan = cboPlan.Text;
  Busy.IsBusy = true;
  Busy.BusyContent = "Sending Request.";
  // You will start populating combobox asynchronously here 
  bw.RunWorkerAsync();

我不確定您要做什么,但是如果您想保存控件的值,請在調用Items.Clear()函數之前執行此操作。

原始答案

我建議您從bw_cboPlan數據庫中填充列表,並在RunWorkerCompletedEventHandler回調函數中填充ComboBox:

List<String> combovalues = new List<String>();
void bw_cboPlan(object sender, DoWorkEventArgs e)
    {
        SqlConnection con = new SqlConnection(Class.GetConnectionString());
        SqlCommand scProduct = new SqlCommand("spSelectProduct", con);
        scProduct.Parameters.Add(new SqlParameter("@Site",site));
        scProduct.CommandType = CommandType.StoredProcedure;
        SqlDataReader readerPortal;
        con.Open();
        readerPortal = scProduct.ExecuteReader();
        combovalues.Clear();
        while (readerPortal.Read())
        {
             combovalues.Add(readerPortal[0]); // untested
            //this.Dispatcher.Invoke((Action)delegate(){cboPlan.Items.Add(readerPortal[0]);});
        }
        con.Close();
    }

    void bw_cboPlanComplete(object sender, RunWorkerCompletedEventArgs e)
    {
        foreach(var item in combovalues)
            cboPlan.Items.Add(item);
        cboPlan.SelectedIndex = 0;
        Busy.IsBusy = false;
    }

暫無
暫無

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

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