title: Can not Detect Changes in Datagrid records bound to EF navigation property MVVM (works with listview)
body: <p>I have a WPF application using the MVVM model and Entity Framework 5 for the data access.  I have a user control that contains a master / detials view of employees.  A listview for the list of employees and a "details" grid with its datacontext set to the selected employee of the listview.  Within that grid I have a datagrid that is bound to a navigation property of the selected employee(property named employee_certification).  All of the bindings work properly and there are no errors, even in the output window.</p>
  
  <p><strong>PROBLEM</strong>:  My problem is that I can not detect if a user makes any changes to the records of the navigation property.  I currently use the change tracker to test for changes in the Entities to enable certain command buttons like save, undo etc.  But The change tracker never detects the changes <strong>within</strong> the navigation property(called employee_certification).</p>
  
  <p>Here is how I get my employee entities from the model:</p>
  
  <pre><code>    Public Function GetEmployee_All(Context As FTC_Context) As ObservableCollection(Of employee) Implements IEmployeeDataService.GetEmployee_All
          Dim employees = Context.employees.Include("employee_certification").ToList            
          Return New ObservableCollection(Of employee)(employees)
      End Function
  </code></pre>
  
  <p>Here is the binding for the navigation property to the datagrid.  (Which works, meaning that it does display the right records)</p>
  
  <pre><code>&lt;DataGrid  Grid.Column="1" Grid.Row="2" MinWidth="350"        
              ItemsSource="{Binding ElementName=DetailControl, Mode=TwoWay, Path=DataContext.employee_certification}" 
              CanUserAddRows="False" CanUserDeleteRows="False" &gt;
      &lt;DataGrid.Columns&gt;
      &lt;/DataGrid.Columns&gt;
  &lt;/DataGrid&gt;
  </code></pre>
  
  <p>Even though the binding works and the right records are displayed, I am unable to determine if the user has made changes to the records of the navigation property.  </p>
  
  <p><strong>GOAL</strong>: I want to be able to detect the changes through the context itself, if possible.  I don't want to have to write a helper class to compare the current records to the the database originals.  Can someone please help me do this?</p>
  
  <p><strong>EDIT #1:</strong>
  I think it might have somthing to do with the datagrid speifically.  I have the following listview in another usercontrol in my app, and any changes made to the record fields in the listviews records can be tracked by the EF context.</p>
  
  <p>listview where the binding vendor_account is a navigation property of entity vandor:</p>
  
  <pre><code> &lt;ListView Grid.Column="1" Grid.Row="2"
             Style="{DynamicResource FTC_SubListView}"
             ItemContainerStyle="{DynamicResource FTC_SubListViewItem}"
             ItemsSource="{Binding vendor_account, Mode=TwoWay}"
             ItemTemplate="{DynamicResource FTC_VendorAccountsTemplate}"  /&gt;
  </code></pre>
  
  <p>Here is how I determine if there are changes to enable the save button in my view:</p>
  
  <pre><code>    Private Function CanSaveExecute() As Boolean
          If _Selection.HasErrors = False Then
              If (From entry In Context.ChangeTracker.Entries(Of vendor)() Where entry.Entity.idVendor = _Selection.idVendor And entry.State = EntityState.Modified Select entry).Count &gt; 0 Then
                  Return True
              ElseIf (From entry In Context.ChangeTracker.Entries(Of vendor_account)() Where entry.Entity.idVendor = _Selection.idVendor And (entry.State = EntityState.Modified Or entry.State = EntityState.Added) Select entry).Count &gt; 0 Then
                  Return True
              Else
                  Return False
              End If
          Else
              Return False
          End If
      End Function
  </code></pre>
  
  <p>but if I use the following for the datagrid, it never has a change count for the emplopyee_certification navigation property:</p>
  
  <pre><code>    Private Function CanSaveExecute() As Boolean
          If _Selection.HasErrors = False Then
              If (From entry In Context.ChangeTracker.Entries(Of employee)() Where entry.Entity.idEmployee = _Selection.idEmployee And entry.State = EntityState.Modified Select entry).Count &gt; 0 Then
                  Return True
              ElseIf (From entry In Context.ChangeTracker.Entries(Of employee_certification)() Where entry.Entity.idEmployee = _Selection.idEmployee And entry.State = EntityState.Modified Select entry).Count &gt; 0 Then
                  Return True
              Else
                  Return False
              End If
          Else
              Return False
          End If
      End Function
  </code></pre>
  
  <p>Does anyone know why my listview allows the change tracker to see x number of chnaged entities from the changetracker but not with the datagrid?</p>
  
  <p><strong>EDIT #2</strong></p>
  
  <p>Okay, so I am narrowing it down, I put a listview and datagrid on the same usercontrol and bound them both the same way to the navigation property of the object's datacontext as described above.  An employee has certifications as linked records, which are the navigation property (employee_certification) of the employee object.  </p>
  
  <p>If I use a listview for the employee_certification records then when I make changes to those records I can query the change tracker to count changes and enable save and undo buttons if the count is more than zero.</p>
  
  <pre><code> From entry In Context.ChangeTracker.Entries(Of employee_certification)() Where entry.Entity.idEmployee = _Selection.idEmployee And entry.State = EntityState.Modified Select entry).Count
  </code></pre>
  
  <p>When I use a datagrid and change any fields in the employee_certification records, the change tracker is not marking any of them as modified.</p>
  
  <p>here is how the datagrid / listview are defined</p>
  
  <pre><code> &lt;DataGrid  Grid.Column="1" Grid.Row="2" MinWidth="350"        
              ItemsSource="{Binding employee_certification, Mode=TwoWay}" 
              CanUserAddRows="False" CanUserDeleteRows="False" &gt;
      &lt;DataGrid.Columns&gt;
          &lt;!--  Custom Defined Columns Go Here  --&gt;
      &lt;/DataGrid.Columns&gt;
  &lt;/DataGrid&gt;
  &lt;ListView Grid.Column="1" Grid.Row="3"
              Style="{DynamicResource FTC_SubListView}"
              ItemContainerStyle="{DynamicResource FTC_SubListViewItem}"
              ItemsSource="{Binding employee_certification, Mode=TwoWay}"
              ItemTemplate="{DynamicResource MyTemplate}"&gt;
  &lt;/ListView&gt;
  </code></pre>
  
  <p>I want to use the datagrid because of built in sorting etc.</p>
  
  <p><strong>QUESTION OF EDIT #2:</strong> Why does the change tracker track changes in listview edits but not datagrid edits?</p>