WPF: ComboBox SelectedIndex = -1?

msfanboy picture msfanboy · Jun 11, 2010 · Viewed 9k times · Source

I am using a MVVM Wizard with several pages. When I set a value in the combobox and go to the next page and switch back I want to reset the value I set before.

But all whats happening is that the combobox is empty at top and the index is -1 ?

What do I wrong?

<ComboBox ItemsSource="{Binding Path=LessonNumbers}" SelectedIndex="{Binding SelectedLessonNumber}" />


 private ReadOnlyCollection<int> _lessonNumbers;
    public ReadOnlyCollection<int> LessonNumbers
    {
        get
        {
            if (_lessonNumbers == null)
                this.CreateLessonNumbers();

            return _lessonNumbers;
        }
    }

    private void CreateLessonNumbers()
    {
        var list = new List<int>();
        for (int i = 1; i < 24; i++)
        {
            list.Add(i);
        }

        _lessonNumbers = new ReadOnlyCollection<int>(list);
    }

    private int _selectedLessonNumber;
    public int SelectedLessonNumber 
    {
        get { return _selectedLessonNumber; }
        set
        {
            if (_selectedLessonNumber == value)
                return;

            _selectedLessonNumber = value;
            this.OnPropertyChanged("SelectedLessonNumber");
        }
    }

UPDATE:

<ComboBox             
        SelectedIndex="0"
        SelectedItem="{Binding SelectedWeeklyRotationNumber}"
        ItemsSource="{Binding Path=WeeklyRotationNumbers}"
        Height="23" 
        HorizontalAlignment="Left"
        Margin="336,212,0,0"
        VerticalAlignment="Top"
        Width="121"
        MaxDropDownHeight="100"
        IsReadOnly="True"
        IsTextSearchEnabled="False"          
        />

private ReadOnlyCollection _weeklyRotationNumbers; public ReadOnlyCollection WeeklyRotationNumbers { get { if (_weeklyRotationNumbers == null) this.CreateWeeklyRotationNumbers();

            return _weeklyRotationNumbers;
        }
    }

    private void CreateWeeklyRotationNumbers()
    {
        var list = new List<string>();

        list.Add("No rotation");
        for (int i = 1; i < 16; i++)
            list.Add(i.ToString());

        _weeklyRotationNumbers = new ReadOnlyCollection<string>(list);
    }

    private string _selectedWeeklyRotationNumber;
    public string SelectedWeeklyRotationNumber
    {
        get { return _selectedWeeklyRotationNumber; }
        set
        {
            if (_selectedWeeklyRotationNumber == value)
                return;

            _selectedWeeklyRotationNumber = value;
            this.RaisePropertyChanged("SelectedWeeklyRotationNumber");

            Messenger.Default.Send<string>(value);
        }
    }

Again, what do I wrong or what is wrong with the string property?

Answer

Zamboni picture Zamboni · Jun 11, 2010

Change XAML SelectedIndex to SelectedItem:

<ComboBox ItemsSource="{Binding Path=LessonNumbers}" 
          SelectedItem="{Binding SelectedLessonNumber}" /> 

UPDATE:

Somewhere you must set the DataContext of your Window to reference the collection from your XAML.

In my case I typically do that in the constructor of my view.

 // this my class containing WeeklyRotationNumbers
 private MainViewModel _mvm;

  public MainView()
  {
     InitializeComponent();

     _mvm = new MainViewModel();
     DataContext = _mvm;
  }

I added string to the read only collections:

  private ReadOnlyCollection<string> _weeklyRotationNumbers;
  public ReadOnlyCollection<string> WeeklyRotationNumbers

I also implemented the interface INotifyPropertyChanged which I think you did, but you are likely using a different base class to handle the PropertyChanged event.

Everthing else I cut and paste from your code.