title: WPF DataGrid - Iterating through rows and accessing control in template column
body: <p>I come from an ASP.NET background, and I've been tasked with maintaining a WPF project, so it's quite a learning curve.</p>
  
  <p>I have a DataGrid with the following XAML</p>
  
  <pre><code>&lt;DataGrid Name="StockGV" AutoGenerateColumns="False" IsReadOnly="True"&gt;
          &lt;DataGrid.Columns&gt;
              &lt;DataGridTemplateColumn Header="Scanned" IsReadOnly="true"&gt;
                  &lt;DataGridTemplateColumn.CellTemplate&gt;
                      &lt;DataTemplate&gt;
                          &lt;Image Name="StatusImage" Source="tick.png"&gt;&lt;/Image&gt;
                          &lt;DataTemplate.Triggers&gt;
                              &lt;DataTrigger Binding="{Binding PalletScanned}" Value="False"&gt;
                                  &lt;Setter TargetName="StatusImage" Property="Source" Value="cross.png"/&gt;
                              &lt;/DataTrigger&gt;
                          &lt;/DataTemplate.Triggers&gt;
                      &lt;/DataTemplate&gt;
                  &lt;/DataGridTemplateColumn.CellTemplate&gt;
              &lt;/DataGridTemplateColumn&gt;
              &lt;DataGridTextColumn Header="Pallet Number" IsReadOnly="True" Binding="{Binding PalletNumber}"&gt;
              &lt;/DataGridTextColumn&gt;
              &lt;DataGridTextColumn Header="Quantity" IsReadOnly="True" Binding="{Binding Quantity}"&gt;
              &lt;/DataGridTextColumn&gt;
              &lt;DataGridTemplateColumn IsReadOnly="true"&gt;
                  &lt;DataGridTemplateColumn.CellTemplate&gt;
                      &lt;DataTemplate&gt;
                          &lt;xctk:IntegerUpDown HorizontalAlignment="Left" Name="integerUpDown1" Maximum="{Binding Quantity}" Minimum="0" VerticalAlignment="Top" Value="{Binding Quantity, Mode=OneWay}" /&gt;
                          &lt;DataTemplate.Triggers&gt;
                              &lt;DataTrigger Binding="{Binding PalletScanned}" Value="False"&gt;
                                  &lt;Setter TargetName="integerUpDown1" Property="IsEnabled" Value="False"/&gt;
                              &lt;/DataTrigger&gt;
                          &lt;/DataTemplate.Triggers&gt;
                      &lt;/DataTemplate&gt;
                  &lt;/DataGridTemplateColumn.CellTemplate&gt;
              &lt;/DataGridTemplateColumn&gt;
          &lt;/DataGrid.Columns&gt;
      &lt;/DataGrid&gt;
  </code></pre>
  
  <p>The user updates the last field (the IntegerUpDown control), and then they click a 'Save' button below the grid. I need this to iterate through each row, get the value of the IntegerUpDown control and save this to the DB. If this was ASP.NET, I'd do something like this:</p>
  
  <pre><code>foreach (GridViewRow row in gv.Rows)
  {
       long pk = (long)gv.DataKeys[row.RowIndex].Value;
       int value = (int)((IntegerUpDown)row.FindControl("integerUpDown1")).Value;
       //Save to DB
  }
  </code></pre>
  
  <p>How would I do this in WPF? Note if it helps, the DataGrid is bound to an anonymous type</p>