title: WPF ComboBox selectedValue binding fails
body: <p>I have an <code>ObservableCollection&lt;Employee&gt;...</code>, an <code>ObservableCollection&lt;Departments&gt;</code> and <code>Enployee</code> is defined as </p>
  
  <pre><code>public class Employee
  {
      [Key]
      public int Id { get; set; }
      public string FirstName { get; set; }
      public string LastName { get; set; }
      public DateTime Birthday { get; set; }
      public int DepId { get; set; }
  
      [ForeignKey("DepId")]
      public virtual Departments Departments { get; set; } 
  
  }
  </code></pre>
  
  <p>and <code>Department</code>is defined as </p>
  
  <pre><code>public class Departments
  {
      [Key]
      public int Id { get; set; }
      public string Name { get; set; }
      public virtual IEnumerable&lt;Employee&gt; Employees { get; set; } 
  }
  </code></pre>
  
  <p>In the database I have </p>
  
  <p><a href="https://i.stack.imgur.com/2Ocog.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/2Ocog.png" alt="enter image description here"></a></p>
  
  <p>It looks like last ComboBox fails to locate the DepId which belongs to the corresponding Employee！！ Any ideas guys?</p>
  
  <pre><code>    &lt;DataGrid  Name="DataGrid1" Grid.Row="3" Margin="10,0,10,10"
                 RenderOptions.ClearTypeHint="Enabled"
                 TextOptions.TextFormattingMode="Display"
                 CanUserAddRows="False"
                 CanUserDeleteRows="False"
                 SelectionUnit="FullRow" 
                 AutoGenerateColumns="false"
                 SelectedItem="{Binding CurrentSelectedEmployee, Mode=TwoWay}"
                 ItemsSource="{Binding Employees, Mode=TwoWay}"&gt;
          &lt;DataGrid.Columns&gt;
              &lt;!--Column 1: Employee Id--&gt;
              &lt;DataGridTextColumn Header="Emplyee Id" Binding="{Binding Id}"/&gt;
  
              &lt;!--Column 2: First Name--&gt;
              &lt;DataGridTextColumn Header="First Name" Binding="{Binding FirstName}"/&gt;
  
              &lt;!--Column 3: Last Name--&gt;
              &lt;DataGridTextColumn Header="Last Name" Binding="{Binding LastName}"/&gt;
  
              &lt;!--Column 4: Birth Day--&gt;
              &lt;DataGridTemplateColumn Header="Birth Day" &gt;
                  &lt;DataGridTemplateColumn.CellTemplate&gt;
                      &lt;DataTemplate&gt;
                          &lt;DatePicker SelectedDate="{Binding Birthday}"  BorderThickness="0" /&gt;
                      &lt;/DataTemplate&gt;
                  &lt;/DataGridTemplateColumn.CellTemplate&gt;
              &lt;/DataGridTemplateColumn&gt;
  
              &lt;!--Column 5: Department Id--&gt;
              &lt;DataGridTemplateColumn Header="Department" &gt;
                  &lt;DataGridTemplateColumn.CellTemplate&gt;
                      &lt;DataTemplate&gt;
                          &lt;ComboBox ItemsSource="{Binding Departments}"
                                    DisplayMemberPath="Name" SelectedValuePath="Id" SelectedValue="{Binding DepId}" /&gt;
                      &lt;/DataTemplate&gt;
                  &lt;/DataGridTemplateColumn.CellTemplate&gt;
              &lt;/DataGridTemplateColumn&gt;
  
          &lt;/DataGrid.Columns&gt;
      &lt;/DataGrid&gt;
  </code></pre>
  
  <p>It looks like last ComboBox fails to locate the DepId which belongs to the corresponding Employee！！ Any ideas guys?</p>
  
  <p><a href="https://i.stack.imgur.com/q4kPi.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/q4kPi.png" alt="enter image description here"></a></p>
  
  <p>UPDATE: My viewModel</p>
  
  <pre><code>public class MainViewModel : ViewModelBase
  {
      private readonly ManagerDbContext _context = new ManagerDbContext();
      public MainViewModel()
      {
  
      }
      private IEnumerable&lt;Departments&gt; _departments;
      public ObservableCollection&lt;Departments&gt; Departments
      {
          get
          {
              return
                  new ObservableCollection&lt;Departments&gt;(_context.Departments);
          }
          set
          {
              _departments = value;
              RaisePropertyChanged("Departments");
          }
      }
  
      private IEnumerable&lt;Employee&gt; _employee;
  
      public IEnumerable&lt;Employee&gt; Employees
      {
          get
          {
              return
                  new ObservableCollection&lt;Employee&gt;(_context.Employees.Include(e =&gt; e.Department));
          }
          set
          {
              _employee = value;
              RaisePropertyChanged("Employees");
          }
      }
      private Employee _currentSelectedEmployee;
      public Employee CurrentSelectedEmployee
      {
          get
          {
              return _currentSelectedEmployee;
          }
          set
          {
              _currentSelectedEmployee = value;
              RaisePropertyChanged("CurrentSelectedEmployee");
          }
      }
  
  }
  </code></pre>