lundi 11 mai 2015

Clearing and refilling a bound combo box

I have a WPF application with a number of comboboxes that tie together. When I switch comboboxx #1, combobox #2 switches, etc.

Here is the xaml for the 2 comboboxes:

<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 gets populated at the beginning and doesnt need a reset. When I do an index change it calls a backgroundworker which calls the following code:

        private void CboCustomerList_DoWork(object sender, DoWorkEventArgs e)
        {
            if (RdoItemSearch.IsChecked == false) return;

            Mouse.OverrideCursor = System.Windows.Input.Cursors.Wait;
            string connectionString = Settings.Default.ProdConnectionString;
            SqlConnection connection = new SqlConnection(connectionString);

            SqlCommand SqlCmd = new SqlCommand();
            SqlCmd.CommandType = CommandType.StoredProcedure;
            SqlCmd.CommandText = "sp_GetItemIds";
            SqlCmd.Parameters.Add("@customer", SqlDbType.NVarChar).Value = CboCustomerList.SelectedValue.ToString().Trim();
            SqlCmd.Connection = connection;
            SqlDataReader reader = null;
            connection.Open();
            reader = SqlCmd.ExecuteReader();

            while (reader.Read())
            {
                CboItemId.Items.Add(reader["EXTITEM"].ToString());
            }
}

The problem is that i am unable to switch selections on CboDivision and have CboCustomerList clear and reload new values into it. Is it the way I'm binding the values in the xaml? How can I have a change in CboDivision cause a clearing of CboCustomerList items, then the execution of the filling routine?

i am currently resetting the combobox with:

CboCustomerList.SelectedIndex = -1;

but this just appends the new cbocustomerlist query to the end I also tried

CboCustomerList.Items.Clear()

but that just returns a null reference error after the box is refilled and the user selects an item.

Aucun commentaire:

Enregistrer un commentaire