title: Find the focused cell from a DataGrid
body: <p>I need to implement a "copy value to clipboard" function for a WPF DataGrid that should be available through all common channels: Right-click context menu item; menu-key context menu item; Ctrl+C hotkey. Data grid content comes from data binding, but since the copy command is a view-only thing, it's implemented completely in the view layer, not the viewmodel. So I don't use ICommand for this but only event handlers in code behind.</p>
  
  <p>The DataGrid's SelectionUnit is set to FullRow, but the arrow navigation keys still work and the focus rectangle can be seen for single cells. So a single cell can be focused while a full row is selected. For this command I need to determine the focused cell.</p>
  
  <p>The menu item is already pretty complicated. Its Click event gives me the MenuItem instance, from which I can navigate to the ContextMenu and further to the DataGrid. That's all, no clicked-on cell. I already know how to get the DataGrid, there's only one of them visible at a time.</p>
  
  <p>I now need to find out which cell is focused. But I can't even get a list of selected cells or rows or even all rows. All properties I can find just point to some fragments of the information, and DataGrid.Rows just doesn't exist. I could scan the entire visual tree for some focused DataGridCell but that's probably not very efficient.</p>
  
  <p>Any ideas?</p>
  
  <p>Later I also need a second function "copy selected rows to clipboard" that copies the values from all cells in all selected rows in a format that can be pasted to Excel (tab-delimited lines).</p>