C# 组合框。文本总是返回空值
我试图从一个组合框中获取当前选中的选项,并尝试通过它获取文本C# 组合框。文本总是返回空值,c#,.net,wpf,combobox,C#,.net,Wpf,Combobox,我试图从一个组合框中获取当前选中的选项,并尝试通过它获取文本 ComboBox.Text Combobox.SelectedItem() 但是.Text返回一个空字符串,SelectedItem()返回null 下面是我如何填充组合框的代码。组合框的值取决于另一个组合框的值 private void cboSite_SelectionChanged(object sender, SelectionChangedEventArgs e) { BackgroundWorker b
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,在的情况下返回空字符串
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\u 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;
}
List combovalues=new List();
void bw_cboPlan(对象发送方,DoWorkEventArgs e)
{
SqlConnection con=新的SqlConnection(Class.GetConnectionString());
SqlCommand scProduct=新的SqlCommand(“spSelectProduct”,con);
添加(新的SqlParameter(“@Site”,Site));
scProduct.CommandType=CommandType.StoredProcess;
SqlDataReader readerPortal;
con.Open();
readerPortal=scProduct.ExecuteReader();
combovalues.Clear();
while(readerPortal.Read())
{
combovalues.Add(readerPortal[0]);//未测试
//调用((操作)委托(){cboPlan.Items.Add(readerPortal[0]);});
}
con.Close();
}
void bw_cboPlanComplete(对象发送方,RunWorkerCompletedEventArgs e)
{
foreach(组合值中的变量项)
cOplan.Items.Add(项目);
cboPlan.SelectedIndex=0;
Busy.IsBusy=false;
}
您的XAML看起来像什么?在哪里以及哪个组合框为所选项目返回null?在RunWorkerCompleted回调中?我的坏帖子,再次编辑。Isplan=cboPlan.Text代码>此处为空?谢谢。。我一直在为这么小的问题绞尽脑汁。。非常感谢!