title: Adding Tooltip to DataGrid Cells programmatically
body: <p>i made different approaches to add tooltips to the cells of a DataGrid. I also found several information on this site, but i didn't make it work.</p>
  <p>Here's what's the matter and what I've tried:</p>
  <p>I have a DataGrid like:</p>
  <pre><code>DataGrid grid = new DataGrid();
  Binding b = new Binding() { Mode = BindingMode.TwoWay, UpdateSourceTrigger = UpdateSourceTrigger.Default, Source = AnObersableCollection, NotifyOnSourceUpdated = true, Path = new PropertyPath(&quot;.&quot;) } );
  
  grid.SetBinding(DataGrid.ItemsSourceProperty, b);
  </code></pre>
  <p>I'd like that every cell has a tooltip with the cell content as tooltip content, so that truncated text is seen in the tooltip. So i took the CellStyles and created one like this:</p>
  <pre><code>Style CellStyle_ToolTip = new Style();
  CellStyle_ToolTip.Setters.Add(new Setter(DataGridCell.ToolTipProperty, new ToolTip() { Content = &quot;Yeah!&quot; } ));
  </code></pre>
  <p>That works for static ToolTip contents, but how can I achieve that the ToolTip has the displayed cell content as content?</p>
  <p>I found out that</p>
  <pre><code>CellStyle_ToolTip.Setters.Add(new Setter(DataGridCell.ToolTipProperty, new ToolTip().SetBinding(ToolTip.ContentProperty, b) ));
  </code></pre>
  <p>does not work and produces an &quot;Cannot set expression. It is marked as &quot;NonShareable&quot; and has already been used&quot;-Error, which makes quite sense as the Binding is already in use. I came to this approach (which probably was complete nonsense) through several other discussions that all use xaml, which is not an option for me. I also found the following solution but don't know how to use without xaml.</p>
  <pre><code>&lt;DataGrid.CellStyle&gt;
      &lt;Style TargetType=&quot;DataGridCell&quot;&gt;
          &lt;Setter Property=&quot;ToolTip&quot; Value=&quot;{Binding RelativeSource={RelativeSource Self}, Path=Content.Text}&quot; /&gt;
      &lt;/Style&gt;
  &lt;/DataGrid.CellStyle&gt;
  </code></pre>
  <p>PS: All columns are DataGridTextColumns, except for one DataGridComboBoxColumn.</p>