Combobox.Text 始终返回 Null
本文关键字:返回 Null Text Combobox | 更新日期: 2023-09-27 17:57:14
我正在尝试从组合框中获取当前选定的选项,并且我尝试通过其文本
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
组合框返回。
这是它在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" />
Edit
好吧,你的代码中有这个:
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
组合框:
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;
}