0

我正在尝试从组合框中获取当前选择的选项,并且尝试通过它获取文本

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组合框中的值,但我无法在代码中使用它们。

编辑:空值由cboPlan组合框返回。

这是当它返回 null 的情况下SelectedItem()和空字符串的情况下.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" />
4

1 回答 1

0

Edit

Well, you have this in your code:

  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();

I am not sure what are you trying to do, but if you want to save value of the control, do that before calling Items.Clear() function.

Original answer

I suggest that you fill List from database in bw_cboPlan, and to fill ComboBox in RunWorkerCompletedEventHandler callback function:

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;
    }
于 2013-02-02T12:43:03.323 回答