0
votes

I have created a very simple ViewModel that contains the following collection

 public ObservableCollection<Reader> Readers 
    { 
        get
        {
            if(_readers == null)
            {
                _readers = new ObservableCollection<Reader>();
            }

            return _readers;
        }

        set
        {
            _readers = value;
        }
    }

and when a number picker is changed I add a Reader to the collection like so:

_activeServer.Readers.Add(Readers.Instance.AllReaders[0]);

I have set the DataContext to be the class containing the Readers collection and I attempt to bind a ListBox of ComboBoxes to the items as follows:

  <ListBox Name="_lbLanes"
                     BorderThickness="0"
                     Height="200"
                     ScrollViewer.HorizontalScrollBarVisibility="Hidden"
                     ItemsSource="{Binding Readers}"    >
                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <ComboBox Name="_cbReaders"
                              Margin="0,0,0,10"
                              ItemsSource="{Binding Source={x:Static models:Readers.Instance}, Path=AllReaders}"
                              DisplayMemberPath="Name"
                              SelectedItem="{Binding Path=., Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
                              MinWidth="400">
                        </ComboBox>
                    </DataTemplate>
                </ListBox.ItemTemplate>

                <ListBox.ItemsPanel>
                    <ItemsPanelTemplate>
                        <StackPanel Orientation="Vertical"/>
                    </ItemsPanelTemplate>
                </ListBox.ItemsPanel>
            </ListBox>

You can see that I am binding to itself and have set the mode to TwoWay.

Also I have tried SelectedValue as well as SelectedItem to be bound and both produce the following problem...

When I click save to commit the page I check the contents of the Readers collection for the item and all are set to its initial value which is equal to Readers.Instance.AllReaders[0] which would be correct I guess if I didn't want two way binding but I do, is this possible or must I go and manually get the items selected within all comboboxes?

Thanks in advance for taking the time to reply

Dan

1

1 Answers

0
votes

I don't think you can bind TwoWay like this.

Maybe this is what you want.

And just because "Two-way binding requires Path or XPath." doesnt mean, that you should set it to same value. You should really create specific object, for your ListBox, that will contain Reader as a property.