title: How to get content of selected DataGrid cell when SelectionUnit=Cell
body: <p>I want to get the value of the cell the user is selecting like so:</p>
  
  <p><a href="https://i.stack.imgur.com/cnXgg.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/cnXgg.png" alt="enter image description here"></a></p>
  
  <p>However it's turning out to be more challenging than I thought it would be. </p>
  
  <p>I've been digging around on the contents of these:</p>
  
  <pre><code>DataGridCellInfo currentCell = MyDataGrid.CurrentCell;
  DataGridCellInfo selectedCell = MyDataGrid.SelectedCells[0];
  
  // object selectedItems = MyDataGrid.SelectedItems[0]; throws index out of range error
  
  object selectedValue = MyDataGrid.SelectedValue; // null
  object selectedItem = MyDataGrid.SelectedItem; // null
  </code></pre>
  
  <p>But I can't find the simple text in any of these. Does anyone know where to get the value "MEH"? Preferably from a <code>DataGridCellInfo</code> type.</p>
  
  <p>Thanks in advance.</p>
  
  <p><strong>Edit:</strong></p>
  
  <p>I managed to get this to work for <code>DataGridTextColumns</code>, but I also have <code>DataGridTemplateColumns</code> and need it to work for those too. </p>
  
  <pre><code>public string GetSelectedCellValue()
  {
      DataGridCellInfo cellInfo = MyDataGrid.SelectedCells[0];
      if (cellInfo == null) return null;
  
      DataGridBoundColumn column = cellInfo.Column as DataGridBoundColumn;
      if (column == null) return null;
  
      FrameworkElement element = new FrameworkElement() { DataContext = cellInfo.Item };
      BindingOperations.SetBinding(element, TagProperty, column.Binding);
  
      return element.Tag.ToString();
  }
  </code></pre>
  
  <p>Any ideas?</p>