MVVM ComboBox SelectedIndex vs. SelectedItem
<p>I am trying to get multiple ComboBoxes populated with data from a specific record.  I can get one of them functioning correctly, but I think a conceptual error is preventing me from getting the second one working right.  I have searched various threads for SelectedItem, SelectedValue, and SelectedIndex and tried fashioning my code after the guidance provided.  Obviously I am doing something wrong, but not sure what.  I am very much a novice with everything I've learned being self-taught from the internet.</p>

<p>I am using an Entity Framework 6 Database First data model.  I have one table (Master) which has a foreign key to link to the Shift and another for the Personnel tables.  Shift table has the fields of ShiftId (PK) which has Identity set to true and Shift which is just a string. Personnel table has PersonnelId (PK) which has Identity set to false (I'm populating the IDs manually from elsewhere) and first and last name fields.</p>

<p>The desired flow of my project is: 1) Get the current record, 2) Populate the Shift ComboBox, 3) Display the shift associated with the current record, 4) Populate the Personnel ComboBox, 5) Display the person associated with the current record, 6) Save any changes to the record (changes established by selecting a different value for either of the ComboBoxes).</p>

<p>My ViewModel is utilizing INotifyPropertyChanged, and I think I have implemented this correctly since the Shift ComboBox is functional.</p>

<p>1) Getting the desired record works fine.  It is then stored as "CurrentRecord"</p>

<p>2) Populating the Shift ComboBox works fine.<br>
In my <strong>Model</strong> I have:</p>

<pre><code>    private ICollection&lt;Gen_All_Shift&gt; shiftList;
    public ICollection&lt;Gen_All_Shift&gt; GetShiftList()
    {
        shiftList = context.Gen_All_Shift.OrderBy(p =&gt; p.ShiftId)
                                             .ToArray();
        return shiftList;
    }
</code></pre>

<p>In my <strong>ViewModel</strong> I have:</p>

<pre><code>    public ICollection&lt;Gen_All_Shift&gt; ShiftList
    {
        get { return context.GetShiftList(); }
    }

    private Gen_All_Shift selectedShift;
    public Gen_All_Shift SelectedShift
    {
        get { return selectedShift; }
        set
        {
            if (value != selectedShift)
            {
                selectedShift = value;
                NotifyPropertyChanged("SelectedShift");
            }
        }
    }
</code></pre>

<p>Then the <strong>XAML</strong> is:</p>

<pre><code>        &lt;ComboBox x:Name="cboShift" 
             ItemsSource="{Binding ShiftList}"
             DisplayMemberPath="Shift"
             SelectedIndex="{Binding CurrentRecord.ShiftIdFk,
                             UpdateSourceTrigger=PropertyChanged}" /&gt;
</code></pre>

<p>3) Displaying the current shift works <strong><em>as long as I have the binding to SelectedIndex</em></strong>.  If I use SelectedItem or SelectedValue, the ComboBox is blank when the screen loads.  (I've also tried binding to SelectedShift.ShiftId but that doesn't give me the desired results.)  SelectedIndex will allow me to change the selection and allows me to properly save the changes to the actual database.  I'm not sure what I am doing wrong that prevents SelectedItem or SelectedValue from working.  Not understanding this is what is causing me to have problems with the personnel combobox I believe.</p>

<p>4) Populating the Personnel ComboBox works.</p>

<p>In my <strong>Model</strong> I have:</p>

<pre><code>    private ICollection&lt;Personnel_All_PersonnelList&gt; personnelList;
    public ICollection&lt;Personnel_All_PersonnelList&gt; GetPersonnelList()
    {            
            personnelList = context.Personnel_All_PersonnelList.Where(p=&gt;p.Active == true)
                                                               .OrderBy(p =&gt; p.LastName)
                                                               .ThenBy(p =&gt; p.FirstName)
                                                               .ToArray();
            return personnelList;
    }
</code></pre>

<p>In my <strong>ViewModel</strong> I have:</p>

<pre><code>    public ICollection&lt;Personnel_All_PersonnelList&gt; PersonnelList
    {
        get
        {
                return context.GetPersonnelList();
        }
    }

    private Personnel_All_PersonnelList selectedPerson;
    public Personnel_All_PersonnelList SelectedPerson
    {
        get { return selectedPerson; }
        set
        {
            if (value != selectedPerson)
            {
                selectedPerson = value;
                NotifyPropertyChanged("SelectedPerson");
            }
        }
    }
</code></pre>

<p>Then in my <strong>XAML</strong> I have:</p>

<pre><code>    &lt;ComboBox x:Name="cboTechnician" 
             ItemsSource="{Binding PersonnelList}"
             DisplayMemberPath="LastName"
             SelectedIndex="{Binding CurrentRecord.PersonnelIdFk,
                             UpdateSourceTrigger=PropertyChanged}" /&gt;
</code></pre>

<p>5) Displaying the current person does not work no matter which way I try (SelectedIndex, SelectedItem, SelectedValue).  I'm guessing the reason SelectedIndex does not work the way it did with the shift is because the index number does not line up properly with the PersonnelId, so the system has no idea which one is actually selected.</p>

<p>6) Saving the changes to the records works if I change the Shift only.  If I select a person in the Personnel ComboBox and try to save, I get a System Null Exception.  I assume this is caused by the same issue that prevents SelectedIndex from working with the personnel combobox.</p>

<p>I think if someone can point me in the right direction as to why I am unable to get the binding to SelectedItem working right, I'll be able to figure out how to fix the Personnel ComboBox.</p>