0

I have two dropdowns in one control which both essentially have the same datasource. At first I had both controls using the same datasource and I also set the combobox attribute "IsSynchronizedWithCurrentItem" to false as I saw in a previous answer somewhat related (Two comboboxes with the same CollectionViewSource ItemSource update each other). Though every time I click on one combobox and then move on to the second one, the first one loses it's values in the drop down, and if I try editing the first box, it changes the values of the second. This time I created a new observable collection for each and made both as indepedent from each other as possible (though the data they get is still the same) but I am still running into the issue. NOTE: The data retrieved is from Entity Framework if that causes any issue with WPF binding:

public void LoadLists() { 
    var items = new ObservableCollection<ComboBoxItem>();
    foreach (var item in Manager.Instance.ItemList) {
        var comboItem = new ComboBoxItem() { Content = item.Name };
        items.Add(comboItem);
    }
    // Binding the two combobox lists
    _viewModel.ListOne = Items;
    _viewModel.ListTwo = new ObservableCollection<ComboBoxItem>(Items);
}

The front end:

<ComboBox IsEditable="True" Text="-- Select --" Name="ListOne" ItemsSource="{Binding ListOne, Mode=OneWay}" IsSynchronizedWithCurrentItem="False" />

...

<ComboBox IsEditable="True" Text="-- Select --" Name="ListTwo" ItemsSource="{Binding ListTwo, Mode=OneWay}" IsSynchronizedWithCurrentItem="False" />

EDIT: SOLVED Thank you TcKs. So I created another combobox in the past using the observablecollection of comboboxitems (from seeing a previous example/Stack Overflow a while back) without any issue. I didn't realize that was the issue. I bound the comboboxes with the source and kept "IsSynchronizedWithCurrentItem" to false and now it works.

Community
  • 1
  • 1
Channafow
  • 707
  • 3
  • 8
  • 17
  • 3
    What... what on earth....... you're binding and manually adding ComboBoxItems.... what? –  Sep 14 '15 at 15:43
  • 2
    The itemsource should not contains ComboBoxItem but the original item. – TcKs Sep 14 '15 at 15:45
  • I don't really understand how they are not independent of each other here. Normally data shown in ComboBox are just for readonly view so that user can see which to provide a selection/choice. If you mean item in combobox can be edited, then you should be clear about what the underlying data is? Why can't the 2 comboboxes showing the same datasource reflect the changes synchronously? That should be an ***expected behavior***. Unless you have ***2 different data sources*** or ***one data source but the view is readonly***. – Hopeless Sep 14 '15 at 15:53
  • Are you trying to use the MVVM pattern? If so, have you tried binding the `SelectedItem` as well? The property on the ViewModel should be of the same type as the item in the ItemSource. I can try to explain further in an answer if you'd like. – Frito Sep 14 '15 at 15:53

1 Answers1

0

Thank you TcKs. So I created another combobox in the past using the observablecollection of comboboxitems (from seeing a previous example/Stack Overflow a while back) without any issue. I didn't realize that was the issue.

I bound the comboboxes with the source (a list of strings) and kept "IsSynchronizedWithCurrentItem" to false and now it works. Binding to an ObservableCollection of comboxboxitems caused the issue.

Channafow
  • 707
  • 3
  • 8
  • 17