WPF: Two comboboxes in the same control are not independent of each other issue

时间:2015-09-14 15:41:26

标签: c# wpf entity-framework combobox

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.

1 个答案:

答案 0 :(得分: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.