title: WPF/PowerShell, copy single cell from a datagrid without header
body: <p>In a WPF DataGrid via PowerShell, I would like the user to be able to copy the cells as follows: 1) if multiple cells and rows are selected selected then copy it with header. 2) If only a single cell is selected copy the cell content without the header. In the sample shown below, I've set ClipboardCopyMode=&quot;IncludeHeader&quot; which enables to copy the selected cells/rows with header which satisfies first requirement indicated above. However, I'm looking for some ideas or quick samples for the second requirement shown above, that is to copy the content without header if only single cell is selected (from context menu or shortcut). I had tried mouse events but couldn't get it to work. Thanks in advance.</p>
  <pre><code>[xml]$xaml=@&quot;
  &lt;Window
      xmlns=&quot;http://schemas.microsoft.com/winfx/2006/xaml/presentation&quot;
      xmlns:x=&quot;http://schemas.microsoft.com/winfx/2006/xaml&quot;
      xmlns:d=&quot;http://schemas.microsoft.com/expression/blend/2008&quot;
      xmlns:mc=&quot;http://schemas.openxmlformats.org/markup-compatibility/2006&quot;
      xmlns:local=&quot;clr-namespace:Test&quot;
      Title=&quot;MainWindow&quot; Height=&quot;425&quot; Width=&quot;550&quot;&gt;
  &lt;Grid&gt;
      &lt;TextBox x:Name=&quot;tb_Search&quot; HorizontalAlignment=&quot;Left&quot; Height=&quot;23&quot; Margin=&quot;10,10,0,0&quot; TextWrapping=&quot;Wrap&quot; VerticalAlignment=&quot;Top&quot; Width=&quot;149&quot;/&gt;
      &lt;Button x:Name=&quot;bt_Search&quot; Content=&quot;Search&quot; HorizontalAlignment=&quot;Left&quot; VerticalAlignment=&quot;Top&quot; Width=&quot;100&quot; IsDefault=&quot;True&quot; Height=&quot;22&quot; Margin=&quot;165,10,0,0&quot; /&gt;        
      &lt;DataGrid x:Name=&quot;dg&quot; Margin=&quot;10,45,0,0&quot; HorizontalAlignment=&quot;Stretch&quot; VerticalAlignment=&quot;Stretch&quot; MinHeight=&quot;100&quot; Height=&quot;Auto&quot; Width=&quot;Auto&quot; ColumnWidth=&quot;Auto&quot; AlternationCount=&quot;1&quot; IsReadOnly=&quot;True&quot; SelectionMode=&quot;Extended&quot; SelectionUnit=&quot;Cell&quot; Background=&quot;White&quot; ClipboardCopyMode=&quot;IncludeHeader&quot;&gt; 
          &lt;DataGrid.ContextMenu&gt;
              &lt;ContextMenu &gt;
                  &lt;MenuItem Command=&quot;{x:Static ApplicationCommands.Copy}&quot; Header=&quot;Copy With Header&quot;/&gt;                                
                  &lt;MenuItem Command=&quot;{x:Static ApplicationCommands.Save}&quot; Header=&quot;Save&quot;/&gt;
              &lt;/ContextMenu&gt;
          &lt;/DataGrid.ContextMenu&gt;
      &lt;/DataGrid&gt;  
  &lt;/Grid&gt;
  &lt;/Window&gt;
  &quot;@
  
  $reader=(New-Object System.Xml.XmlNodeReader $xaml)
  $Window=[Windows.Markup.XamlReader]::Load($reader)
  
  #Turn XAML into PowerShell objects
  $xaml.SelectNodes(&quot;//*[@*[contains(translate(name(.),'n','N'),'x:Name')]]&quot;) | ForEach-Object{
  Set-Variable -Name ($_.Name) -Value $Window.FindName($_.Name)
  }
  
  #sample data
  $DataSet = New-Object System.Data.DataSet
  $Table = $DataSet.Tables.Add(&quot;Table&quot;)
  $Properties = @(&quot;Country&quot;,&quot;Capital&quot;,&quot;Population&quot;)
  $Properties | foreach {
   $Column = New-Object System.Data.DataColumn($_)
   $Table.Columns.Add($Column)
   }
  $Null=$Table.Rows.Add(&quot;China PR&quot;,&quot;Beijing&quot;,&quot;20,693,000&quot;)
  $Null=$Table.Rows.Add(&quot;India&quot;,&quot;New Delhi&quot;,&quot;16,787,949&quot;) 
  $Null=$Table.Rows.Add(&quot;Japan&quot;,&quot;Tokyo&quot;,&quot;13,189,000&quot;)
  $Null=$Table.Rows.Add(&quot;Philippines&quot;,&quot;Manila&quot;,&quot;12,877,253&quot;)
  $Null=$Table.Rows.Add(&quot;Russia&quot;,&quot;Moscow&quot;,&quot;11,541,000&quot;)
  $Null=$Table.Rows.Add(&quot;Egypt&quot;,&quot;Cairo&quot;,&quot;10,230,350&quot;)
  $Null=$Table.Rows.Add(&quot;USA&quot;,&quot;Washington, D.C&quot;,&quot;658,893&quot;)
  $Null=$Table.Rows.Add(&quot;China PR&quot;,&quot;Beijing&quot;,&quot;20,693,000&quot;)
  $Null=$Table.Rows.Add(&quot;India&quot;,&quot;New Delhi&quot;,&quot;16,787,949&quot;) 
  $Null=$Table.Rows.Add(&quot;Japan&quot;,&quot;Tokyo&quot;,&quot;13,189,000&quot;)
  $Null=$Table.Rows.Add(&quot;Philippines&quot;,&quot;Manila&quot;,&quot;12,877,253&quot;)
  $Null=$Table.Rows.Add(&quot;Russia&quot;,&quot;Moscow&quot;,&quot;11,541,000&quot;)
  $Null=$Table.Rows.Add(&quot;Egypt&quot;,&quot;Cairo&quot;,&quot;10,230,350&quot;)
  $Null=$Table.Rows.Add(&quot;USA&quot;,&quot;Washington, D.C&quot;,&quot;658,893&quot;)
  
  #populate datagrid
  $DataView = New-Object System.Data.DataView($Table)
  $array = New-Object System.Collections.ArrayList
  [void] $array.AddRange($DataView)       
  $dg.clear()
  $dg.ItemsSource = $array
  $dg.IsReadOnly = $true
  
  $bt_Search.Add_Click({
  $SearchValue = $tb_Search.text
  for ($i = 0; $i -lt $dg.Items.Count; $i++)
  {
      if ($dg.Items[$i].Row[$dg.Columns.DisplayIndex] -eq &quot;$SearchValue&quot;)
      {
          [System.Windows.Forms.MessageBox]::Show(&quot;Keyword Found&quot;)
          $dg.ScrollIntoView($dg.Items[$i]) #scroll to the row that contains the keyword searched
      }
  }
  })
  
  #Display Form
  $Window.ShowDialog() | Out-Null
  </code></pre>