Combobox.Text总是返回Null

时间:2013-02-02 12:26:31

标签: c# .net wpf combobox

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

获取文本
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 Combobox返回。

以下是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();
        }

The Comboboxes的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" />

1 个答案:

答案 0 :(得分:0)

修改

嗯,你的代码中有这个:

  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中填写数据库中的List,并在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;
    }