2
votes

I am working on this problem for about a day now.

For some reason I am unable to TwoWay bind a value to a ComboBox if it is inside a ItemsControl. Outside works just fine.

I have an ObservableCollection of int? in my ViewModel:

    private ObservableCollection<int?> _sorterExitsSettings = new ObservableCollection<int?>();
    public ObservableCollection<int?> SorterExitsSettings
    {
        get { return _sorterExitsSettings; }
        set
        {
            if (_sorterExitsSettings != value)
            {
                _sorterExitsSettings = value;
                RaisePropertyChanged("SorterExitsSettings");
            }
        }
    }

My XAML:

<ItemsControl ItemsSource="{Binding SorterExitsSettings}">
<ItemsControl.ItemTemplate>
    <DataTemplate>
            <ComboBox ItemsSource="{Binding RelativeSource={RelativeSource AncestorType=ItemsControl}, Path=DataContext.ScanRouter.Stores}"
                        SelectedValue="{Binding Path=., Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" DisplayMemberPath="name" SelectedValuePath="id" IsEditable="True" />
    </DataTemplate>
</ItemsControl.ItemTemplate>

So the ComboBox is populated with a list of stores. It works fine so far. The ObservableCollection SorterExitsSettings even has some values set which are shown in the displayed ComboBoxes. So setting the SelectedValue also works.

However when I change a selection, SorterExitsSettings wont change. While when I implement the ComboBoxes(100) without an ItemsControl it suddenly works fine.

<ComboBox ItemsSource="{Binding ScanRouter.Stores}" DisplayMemberPath="name" SelectedValuePath="id" IsEditable="True" SelectedValue="{Binding SorterExitsSettings[0], Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>

Even better when I implement the ComboBoxes using the ItemsControl and also the example ComboBox shown above. When I change the single ComboBox's value it will change the value of the ComboBox inside the ItemsControl, but not the other way around.

Did somebody encounter this problem before?

My guess was that the ItemsControl doesn't like the fact that I am binding my selected value to an item in a list. However when I bind directly to a ViewModel property(Store) it also doesn't work. I also tried using SelctedItem instead of SelectedValue and populate the ObservableCollection with Store objects instead of int?.

1
This is pretty confusingly worded, and lacks important information. What is "ScanRouter", "Stores", and what does "name" and "id" point to on "Stores"? I've never had issues with ComboBoxes inside an ItemsControl. I don't think using a collection of int? is necessarily wrong.user3690202

1 Answers

2
votes

The problem is that you're binding your ComboBox's SelectedValue directly to the collection elements which are type int ?. This won't work, binding targets have to be properties. Try wrapping your int ? values in a class and expose the value as a property of that class with a getter and setter, i.e. something like this:

private ObservableCollection<Wrapper> _sorterExitsSettings = new ObservableCollection<Wrapper>();
... etc...

And:

public class Wrapper
{
    public int? Value {get; set;}
}

And finally:

<ComboBox ... SelectedValue="{Binding Path=Value, Mode=TwoWay...

Post back here if you still have problems.