C# 清除和重新填充绑定的组合框
我有一个WPF应用程序,其中有许多组合框连接在一起。当我切换combobox X#1、combobox#2开关时,等等 以下是两个组合框的xaml:C# 清除和重新填充绑定的组合框,c#,wpf,xaml,combobox,C#,Wpf,Xaml,Combobox,我有一个WPF应用程序,其中有许多组合框连接在一起。当我切换combobox X#1、combobox#2开关时,等等 以下是两个组合框的xaml: <ComboBox Grid.Row="1" Height="23" HorizontalAlignment="Right" Margin="0,12,286,0" ItemsSource="{Binding}" Name="CboDivision" VerticalAlignment="Top" Width="120" SelectionC
<ComboBox Grid.Row="1" Height="23" HorizontalAlignment="Right" Margin="0,12,286,0" ItemsSource="{Binding}" Name="CboDivision" VerticalAlignment="Top" Width="120" SelectionChanged="CboDivision_SelectionChanged" />
<ComboBox Height="23" HorizontalAlignment="Right" Margin="0,9,32,0" Name="CboCustomerList" ItemsSource="{Binding}" VerticalAlignment="Top" Width="120" SelectionChanged="CboCustomerList_SelectionChanged" Grid.Row="1" />
当我进行索引更改时,它将调用backgroundworker,后者将调用以下代码:
private void FillCustomers(object sender, DoWorkEventArgs e)
{
string connectionString = Settings.Default.ProdConnectionString;
SqlConnection connection = new SqlConnection(connectionString);
SqlCommand SqlCmd = new SqlCommand();
Mouse.OverrideCursor = System.Windows.Input.Cursors.Wait;
SqlCmd.CommandType = CommandType.StoredProcedure;
SqlCmd.Parameters.Add("@division", SqlDbType.NVarChar).Value = division;
SqlCmd.Connection = connection;
SqlCmd.CommandText = "sp_GetCustomers";
SqlDataReader reader = null;
connection.Open();
reader = SqlCmd.ExecuteReader();
List<string> result = new List<string>();
while (reader.Read())
{
result.Add(reader["NAME"].ToString());
}
e.Result = result;
}
但这只是将新的cbocustomerlist查询附加到末尾
我也试过了
CboCustomerList.Items.Clear()
但是,在重新填充框并且用户选择一个项目之后,这只会返回一个空引用错误。有一些代码的详细信息您没有显示。但是,这里有一个有效的示例:两个组合,一个是在开始时填充的组合,另一个是在第一个组合中每次选择更改时填充的组合。请注意,数据是在后台工作程序中获取的,与您的情况相同 XAML
C#
公共部分类主窗口:窗口
{
私有BackgroundWorker bw=新BackgroundWorker();
公共主窗口()
{
初始化组件();
CboDivisions.DataContext=新列表(){“红色”、“蓝色”、“绿色”};
CboDivisions.SelectionChanged+=CboDivisions\u SelectionChanged;
bw.DoWork+=bw_DoWork;
bw.RunWorkerCompleted+=bw_RunWorkerCompleted;
}
void CboDivisions\u SelectionChanged(对象发送者,selectionchangedventargs e)
{
var division=CboDivisions.SelectedValue作为字符串;
运行工作同步(分部);
}
void bw_RunWorkerCompleted(对象发送方,RunWorkerCompletedEventArgs e)
{
CboList.DataContext=e.结果为列表;
}
void bw_DoWork(对象发送方,DoWorkEventArgs e)
{
var division=e.参数作为字符串;
var r=新的随机变量();
var result=新列表();
对于(int i=0;i
好吧,您没有发布所有代码,但一个问题是您没有调用工作线程
将FillCustomers(null,null)
替换为customerWorker.RunWorkerAsync()
private void CboDivision\u SelectionChanged(对象发送者,selectionchangedventargs e)
{
division=CboDivision.SelectedValue.ToString();
CboCustomerList.ItemsSource=null;
BackgroundWorker customerWorker=新的BackgroundWorker();
customerWorker.DoWork+=新的DoWorkerVenthandler(FillCustomers);
customerWorker.RunWorkerCompleted+=新的RunWorkerCompletedEventHandler(customerWorker\u RunWorkerCompleted);
customerWorker.RunWorkerAsync();//替换错误的FillCustomers(null,null);
code
使用:customerWorker.RunWorkerAsync()
在e.Result=Result;上,我得到了一个空引用错误。另外,我应该显示什么详细信息才能得到答案?这意味着你没有复制我显示的代码。我在发布到这里之前已经测试过。不幸的是,我无法附加文件来显示整个项目,但除此之外没有其他内容。不是吗抛出NullReferenceException
是因为FillCustomers
方法中的最后一条指令,以及在CboDivision\u selection中调用FillCustomers(null,null)
方法时发生了更改(第二条null
被分配给e
参数)。这样,您实际上没有使用worker-为此,您应该调用customerWorker.RunWorkerAsync()
相反。@Matt Jacobi说得对,您的工作线程从未启动,或者是否缺少一些代码?如果您实际上调用了工作线程,那么处理程序中会发生什么事情来完成它?您没有将ItemsSource设置为null,而是尝试清空它绑定到的集合吗?
CboCustomerList.SelectedIndex = -1;
CboCustomerList.Items.Clear()
<Window x:Class="CombosRefresh.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<ComboBox Name="CboDivisions" ItemsSource="{Binding}" Grid.Row="0" Margin="5" />
<ComboBox Name="CboList" ItemsSource="{Binding}" Grid.Row="1" Margin="5" />
</Grid>
</Window>
public partial class MainWindow : Window
{
private BackgroundWorker bw = new BackgroundWorker();
public MainWindow()
{
InitializeComponent();
CboDivisions.DataContext = new List<string>() { "red", "blue", "green" };
CboDivisions.SelectionChanged += CboDivisions_SelectionChanged;
bw.DoWork += bw_DoWork;
bw.RunWorkerCompleted += bw_RunWorkerCompleted;
}
void CboDivisions_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
var division = CboDivisions.SelectedValue as string;
bw.RunWorkerAsync(division);
}
void bw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
CboList.DataContext = e.Result as List<string>;
}
void bw_DoWork(object sender, DoWorkEventArgs e)
{
var division = e.Argument as string;
var r = new Random();
var result = new List<string>();
for(int i = 0; i < r.Next(0, 10); ++i)
{
result.Add(string.Format("{0} #{1}", division, i+1));
}
e.Result = result;
}
}
private void CboDivision_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
division = CboDivision.SelectedValue.ToString();
CboCustomerList.ItemsSource = null;
BackgroundWorker customerWorker = new BackgroundWorker();
customerWorker.DoWork += new DoWorkEventHandler(FillCustomers);
customerWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(customerWorker_RunWorkerCompleted);
customerWorker.RunWorkerAsync(); // <-- this
}