views:

352

answers:

1

Hello,

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?

+2  A: 

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.

Zamboni
ah now I know from where the -1 comes because I havent set SelectedIndex = 0 in XAML so nothing is selected means empty there its -1 like it is with the DataGrids Rowhandle if nothing is selected :)With SelectedItem and SelectedIndex = 0 it works properly when I switch back some pages, the user selection is saved!
msfanboy
thats odd your solution does not work when I have string properties see my updated initial post!
msfanboy
Take a look at my update, I fixed a few things starting from your code.
Zamboni
Hello Zamboni,uh thats all odd because my SelectedLessonNumber Property is in the same class as the SelectedWeeklyRotationNumber. So both consume the propertychange event and both comboboxes consume the same datacontext/ViewModel via........<pre><DataTemplate DataType="{x:Type ViewModel:WizardPageLessonTimesViewModel}"> <View:WizardLessonTimesPageView /> </DataTemplate></code>
msfanboy
damn why do the pre/code tags not work in the comment field? I already saw other user using those tags in the comment field and the syntax was highlighted, you have any idea?
msfanboy
OK, I found the bug: It really seems the SelectedIndex property behaves different on int/string datatypes.When I remove in my XAML the SelectedIndex = 0 for the SelectedWeeklyRotationNumber_ComboBox and set instead in the ViewModel Ctor this: SelectedWeeklyRotationNumber = "No rotation";Everything works fine, the user selections are all saved when I switch forth/back the wizard pages.You can explain that?
msfanboy