title: How do I get the last row of a bound WPF Datagrid to notify View Model when the SelectedItem Changes?
body: <p>This is my first post - I welcome any advice on how I post questions should I need it.</p>
  
  <p>I have a WPF <code>DataGrid</code> that has <code>ItemsSource</code> bound to a property called OrderDetails(List Of OrderDetails) and <code>SelectedItem</code> bound to a property called CurrentOrderDetail.  </p>
  
  <p>Inside the CurrentOrderDetail property I call a method that does a calculation.  The method that does the calculation loops through the OrderDetails property and multiplies two members of each OrderDetail that belongs to that list to produce a total cost.  That method just simply updates another property that a text box is bound to on the form.  </p>
  
  <p>This all works great - until I get to the last row of the <code>DataGrid</code>.  Basically the user starts at the first row, clicks into the Invoiced field and enters a quantity and presses enter, which selects the next row.  The <code>SelectedItem</code> changes and therefore updates the CurrentOrderDetail - calling calculate.  This works!  The user repeats the process, everything calculating OK, until the last record in the <code>DataGrid</code> is hit.  When the user hits Enter to end the edit on the Invoiced cell, the calculate method is never called - because the <code>SelectedItem</code> has not changed and therefore the CurrentOrderDetail property is never updated.  </p>
  
  <p>I like standards and since I've recently moved to WPF/Silverlight and MVVM, I've not looked back.  As such, I'm trying to keep this app the same way.  </p>
  
  <p>I guess I need to force something to trigger the <code>SelectedItem</code> changing.  I've thought about hooking into events in the View and simply forcing a navigate, but that just seems a bit ugly to me.  There's gotta be a better solution.  I've spend two days out here and other forums as well as trying several ideas of my own, none of which work.  </p>
  
  <p>I could just ask the user to navigate to another row to trigger the property notification, but I'd rather not.  </p>
  
  <p>Here's just the XAML for the <code>DataGrid</code>:</p>
  
  <pre><code>&lt;DataGrid Name="grdData" Grid.Row="3" Grid.ColumnSpan="2" AutoGenerateColumns="False" CanUserAddRows="False" Margin="5,5,5,5" ItemsSource="{Binding Path=OrderDetails, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"  SelectedItem="{Binding Path=CurrentOrderDetail, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" BorderThickness="3" &gt;
      &lt;DataGrid.Columns&gt;   
          &lt;DataGridTextColumn Binding="{Binding Path=Part}" Header="Wyandot Part #" IsReadOnly="True" /&gt;
          &lt;DataGridTextColumn Binding="{Binding Path=SupplierPartNumber}" Header="Supplier Part #" IsReadOnly="True" /&gt;
          &lt;DataGridTextColumn Binding="{Binding Path=UnitPrice, StringFormat=c}" Header="Price" IsReadOnly="True" /&gt;
          &lt;DataGridTextColumn Binding="{Binding Path=Account}" Header="Account" IsReadOnly="True" /&gt;
          &lt;DataGridTextColumn Binding="{Binding Path=UDF5, Mode=TwoWay}" Header="Invoiced" IsReadOnly="False" /&gt;
      &lt;/DataGrid.Columns&gt;
  &lt;/DataGrid&gt;
  </code></pre>
  
  <p>And the snippets from my View Model that are pertinent.  </p>
  
  <pre><code>Private m_orderDetails As List(Of OrderDetail)
  Public Property OrderDetails As List(Of OrderDetail)
      Get
          Return m_orderDetails
      End Get
      Set(value As List(Of OrderDetail))
          m_orderDetails = value
          InvokePropertyChanged("OrderDetails")
      End Set
  End Property
  
  Private m_currentOrderDetail As OrderDetail
  Public Property CurrentOrderDetail As OrderDetail
      Get
          Return m_currentOrderDetail
      End Get
      Set(value As OrderDetail)
          m_currentOrderDetail = value
          InvokePropertyChanged("CurrentOrderDetail")
          Calculate()
      End Set
  End Property
  
  Public Sub Calculate()
      Dim total As Decimal = 0
  
      For Each detail As OrderDetail In OrderDetails
          total += detail.UnitPrice * detail.UDF5
      Next
  
      PoTotal = CStr("$" &amp; total.ToString)
  End Sub
  </code></pre>