title: How do I copy WPF DataGrid seleted cells to the clipboard?
body: <p>I have a WPF DataGrid the following definition:</p>
  
  <pre><code>&lt;DataGrid Name="DataGridFoo"
    AutoGenerateColumns="False"
    ItemsSource="{Binding GridData}"
    IsReadOnly="True"
    SelectionMode="Extended"
    SelectionUnit="CellOrRowHeader"
    CopyingRowClipboardContent="DataGridFoo_CopyingRowClipboardContent"&gt;
  </code></pre>
  
  <p>This allows me to have the user selected a "region" of cells.
  The DataGrid is bound to an observable collection.
  The XAML column definitions have some columns hidden, some visible
  like this:</p>
  
  <pre><code>&lt;DataGridTextColumn Binding="{Binding InvoiceID}"
     Header="Invoice ID"
     Visibility="Hidden"
     Width="Auto"/&gt;                
  &lt;DataGridTextColumn Binding="{Binding InvoiceNumber}"
     Header="Invoice Number"
     Visibility="Visible"
     Width="Auto"/&gt;
  &lt;DataGridTextColumn 
     Binding="{Binding InvoiceDate,    StringFormat=\{0:MM/dd/yy\}}"                                     
     Header="Invoice Date"
     Visibility="Visible"
     Width="Auto"/&gt;
  </code></pre>
  
  <p><strong>Consider the "irregular" selection of cells in the DataGrid:</strong></p>
  
  <p><a href="https://i.stack.imgur.com/Gw19M.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Gw19M.png" alt="enter image description here"></a></p>
  
  <p>I can see this which shows some promise:</p>
  
  <pre><code>((System.Windows.Controls.VirtualizedCellInfoCollection)DataGridFoo.SelectedCells)._regions
  </code></pre>
  
  <p>I can examine it's contents through Visual Studio, but can seem to figure out how to access that information programmatically.</p>
  
  <p>I would like to be able copy those cells (in a row/column arrangement perhaps) to the clipboard. This data will be copied both within our app and to external applications.</p>
  
  <p>How would I accomplish this?</p>
  
  <p>Thanks</p>