views:

185

answers:

4

This code give me an Argument out of Range exception. When I remove the binding to the SelectedIndex, the combobox is populated just fine and no exception is thrown.

Any Idea what I am doing wrong? Is this (for some reason) not possible?

 public class RuleMap<T> : INotifyPropertyChanged
    {
        public ObservableCollection<string> Options
        {
           get{
                return new ObservableCollection(){"A", "B", "C"};
              }
        }

        public int SelectedIndex
        {
            get{
               return 0;
            }
        }
    }

    public ObservableCollection<RuleMap> FilterItemSource;

    <ItemsControl ItemsSource="{Binding FilterItemSource}">
               <ItemsControl.ItemTemplate>
                <DataTemplate>
                 <StackPanel Orientation="Horizontal">               
                   <ComboBox Width="150" SelectedIndex="{Binding SelectedIndex}" ItemsSource="{Binding Options}"/>
                  </StackPanel>
                </DataTemplate>
               </ItemsControl.ItemTemplate>
              </ItemsControl>
+1  A: 

I guess that SelectedIndex it is a ReadOnly Property.
Other problem can be that 0 it's not in the collection

Eduardo Molteni
+1  A: 

I think that Items are not added before selectedIndex is Binded, and since there are no item, it is showing Argument out of Range exception.

viky
This is true, however not the solution to the problem :)
eskerber
A: 

I would avoid returning a new collection from your Options property. You're making an assumption that WPF only accesses the property once.

But you also have the option of using a CollectionView where you're currently returning an ObservableCollection. If you're using a MVVM architecture, your ViewModel can expose the property as CollectionView and it has the notion of a "current" item.

Josh Einstein
+1  A: 

Turns out the ComboBox control was fundamentally broken to begin with. Thanks to this Blog Post by Rockford Lhotka, we were able to override the ComboBox control with one that could correctly bind to SelectedItem property.

Ick.

eskerber