清除并重新填充绑定的组合框

时间:2015-05-08 21:04:53

标签: c# wpf xaml combobox

我有一个WPF应用程序,其中包含多个组合在一起的组合框。当我切换组合框#1,组合框#2开关等

这是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" />

CboDivision在开始时填充,不需要重置。 HEre是调用除法变更的代码,它应该触发客户的变更:

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);
    FillCustomers(null, null);

}

当我进行索引更改时,它会调用一个调用以下代码的后台工作程序:

    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;


   }

问题是我无法在CboDivision上切换选择并清除CboCustomerList并将新值重新加载到其中。这是我绑定xaml中的值的方式吗? 如何更改CboDivision导致清除CboCustomerList项,然后执行填充例程?

我目前正在使用以下方法重置组合框:

CboCustomerList.SelectedIndex = -1;

但这只是将新的cbocustomerlist查询追加到最后 我也试过

CboCustomerList.Items.Clear()

但在重新填充框并且用户选择项目后,它只返回空引用错误。

3 个答案:

答案 0 :(得分:4)

嗯,你没有发布所有代码,但有一个问题是你没有调用你的工作线程。

FillCustomers(null, null)替换为customerWorker.RunWorkerAsync()

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
}

如果这有帮助,请告诉我。如果还有更多问题,请发布其余代码。

答案 1 :(得分:3)

您没有显示代码的详细信息。但是,这是一个有效的示例:两个组合,开头填充的内容,以及每次选择在第一个中更改时填充的第二个组合。请注意,数据是在后台工作程序中获取的,就像您的情况一样。

XAML

<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>

C#

   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;
      }
   }

答案 2 :(得分:2)

替换错误的FillCustomers(null, null);代码  用:customerWorker.RunWorkerAsync();