title: Unable to Obtain Template Column ComboBox Value in C# WPF
body: <p>I'm trying to obtain a cell value in a a DataGrid table, but am given an error after setting up the column as a TemplateColumn. Before, I had the column just as a ComboBoxColumn, and my code worked.</p>
  
  <p>Here is my C# Code:</p>
  
  <pre><code>DataGridRow Row = (DataGridRow)MYGRID.ItemContainerGenerator.ContainerFromIndex(MYGRID.SelectedIndex);
  RowColumn = MYGRID.Columns[4].GetCellContent(Row).Parent as DataGridCell;
  string Value = ((ComboBox)RowColumn.Content).Text;
  </code></pre>
  
  <p>Here is my XAML Code for the Column:</p>
  
  <pre><code>        &lt;DataGridTemplateColumn x:Name="ValueCol" Header="Value" &gt;
              &lt;DataGridTemplateColumn.CellTemplate&gt;
                  &lt;DataTemplate&gt;
                      &lt;ComboBox   ItemsSource="{Binding Options, UpdateSourceTrigger=PropertyChanged}" SelectedItem="{Binding Path=UpdatedData, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, NotifyOnTargetUpdated=True}" IsEditable="False" LostFocus="ComboBox_LostFocus"  /&gt;
                  &lt;/DataTemplate&gt;
              &lt;/DataGridTemplateColumn.CellTemplate&gt;
          &lt;/DataGridTemplateColumn&gt;
  </code></pre>
  
  <p>When updating the value, I get the following error:</p>
  
  <pre><code>'Unable to cast object of type 'System.Windows.Controls.ContentPresenter' to type 'System.Windows.Controls.ComboBox'.'
  </code></pre>
  
  <p><strong>EDIT</strong>: Was able to obtain the new value by creating a LostFocus event and pulling the data from the RoutedEventArgs object.</p>