添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

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; }
            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?.

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 Aug 25, 2015 at 11:09

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...
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.

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.