views:

729

answers:

2

Hello, Thanks in advance for any help. I've got two combo boxes which I'd like to be dependent on each others input. For example, when I update one, it filters the available values for the next. Thought I had everything working until after I switched between the two a few times, both just because blank.

I've got two combo boxes with countries, FromCountry and ToCountry. These are bound to an ObservableCollection called Countries. I thought it might make sense to use the DroDownOpened event to limit the values available in the dropping down combobox.

Any suggestions or help would greatly be appreciated.

Method that gets called when you click the drop down on the To Country

private void ToCountry_DropDownOpened(object sender, EventArgs e)
{
            int FromId = (Countries.Country)FromCountry.selectedItem).ID;
            int ToId = (Countries.Country)ToCountry.selectedItem).ID;


  this.ToCountry.ItemsSource = AppInfo.CountryList.Where(p=>p.ID!=FromId).OrderBy(c => c.Name);
}

Method that gets called when you click the drop down on the From Country

private void FromCountry_DropDownOpened(object sender, EventArgs e)
{
            int FromId = (Countries.Country)FromCountry.selectedItem).ID;
            int ToId = (Countries.Country)ToCountry.selectedItem).ID;


  this.FromCountry.ItemsSource = AppInfo.CountryList.Where(p=>p.ID!=ToId).OrderBy(c => c.Name);
}
A: 

Have you tried having two ObservableCollections ToCountries and FromCountries and then reacting to the selection changed events to populate?

Graeme Bradbury
I've tried that.It gets me closer but the main issue I'm having is keeping the selected value.For example I have a list of states. NY,NJ, PA. When the user selects PA in the first it should remove it from the next. But, leave the value. When I update the collection I lose my initial selected values.
Ben Bahrenburg
A: 

After some experimentation I managed to find a working solution. The below allows me to have two comboboxes dependent on the same datasource, as well as each other. I’m sure elegant approach, but this seems to work.

1) On my ObservableCollection I added two additional properties, ToSelected and FromSelected.

2) Then instead of binding to the Comboboxes, I populated them directory through a loop. Not the best way to do this, but it seemed to be the most straight forward.

                foreach (Countries.Country currentCnty in CountryList)
                {
                    ComboBoxItem toItem = new ComboBoxItem();
                    toItem.Content = currentCnty;
                    CmboToCountry.Items.Add(toItem);
                    ComboBoxItem fromItem = new ComboBoxItem();
                    fromItem.Content = currentCnty;
                    CmbFromCountry.Items.Add(fromItem);
                }

3) I then created method called UpdateLogic. This simply loops through the ObservableCollection setting the selected records for the From and To comboboxes.

4) Then on selection change for each combobox I added the following to set the visibility other combobox items. For example the To Country combobox would set the visibility of the From Country items and vice versa.

                //Update the underlying collection with selected items
            UpdateLogic((Countries.Country)CmboToCountry.Content, (Countries.Country)CmboFromCountry.Content);

            for (int iLoop = 0; iLoop < CmboToLocation.Items.Count; iLoop++)
            {
                ComboBoxItem currentItem = CmboToCountry.Items[iLoop] as ComboBoxItem;
                Countries.Country currentCountry = (Countries.Country)currentItem.Content;
                currentItem.Visibility = ((currentCountry.IsFrom) && (currentCountry.ID > 0))
                                            ? Visibility.Collapsed : Visibility.Visible;
            }

The business requirement of having two inter-dependent comboboxes has been an interesting challenge. Hopefully this helps someone in the future. Though I’m sure there is a more elegant solution to perform this task.

Ben Bahrenburg