views:

16

answers:

1

I have code like this:

<ListBox ItemsSource="{Binding}">
   <ListBox.ItemTemplate>
      <DataTemplate>
          <StackPanel>
              <TextBlock>Some Other Stuff Here</TextBlock>
              <ComboBox ItemsSource="{Binding}" />
          </StackPanel>
      </DataTemplate>
   </ListBox.ItemTemplate>
</ListBox>

The problem is, every time the outside ListBox.SelectedItem gets changed, the ComboBoxes inside it would change their SelectedIndex to -1. Which means if I click "Some Other Stuff Here" (unless the ListBoxItem it is in is selected), all the comboboxes' selection get cleared.

How do I overcome this? Thx!

A: 

Presumably your combobox is bound to something like an ObservableCollection - try exposing an instance of ICollectionView instead:

class DataSource
{
    // ...

    public ObservableCollection<string> MyData { get; private set; }
    public ICollectionView MyDataView
    {
        get
        {
            return CollectionViewSource.GetDefaultView(this.MyData);
        }
    }
}

You can then bind your combobox with:

<ComboBox ItemsSource="{Binding MyDataView}" IsSynchronizedWithCurrentItem="True" />

This means that the 'selected item' for each data source is stored in the ICollectionView object instead of within the combobox, which should mean that it is persisted when the ListBox SelectedItem changes

Steve Greatrex