Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/306.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 清除和重新填充绑定的组合框_C#_Wpf_Xaml_Combobox - Fatal编程技术网

C# 清除和重新填充绑定的组合框

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

我有一个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" 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
}