title: How to define the DataContext in a complex tooltip on a XAML ListView
body: <p>I want to show a list of items in a WPF ListView and use a tooltip to show additional details of an item in the list.</p>
  
  <p>I have made a sample project which you can download from <a href="http://www.jollans.com/WpfTooltipTest.zip" rel="nofollow">http://www.jollans.com/WpfTooltipTest.zip</a></p>
  
  <p>For the sample I have made a class with some properties:</p>
  
  <pre><code>class ItemClass
  {
    public String    mainProperty_1 { get; set; }
    public String    mainProperty_2 { get; set; }
    public String    detailProperty_1 { get; set; }
    public String    detailProperty_2 { get; set; }
  }
  </code></pre>
  
  <p>and a model which contains a list of these items</p>
  
  <pre><code>class Model
  {
    public   ObservableCollection&lt;ItemClass&gt;  Items { get; private set; }
  
    public Model()
    {
      Items = new ObservableCollection&lt;ItemClass&gt;() ;
      Items.Add ( new ItemClass { mainProperty_1 = "One",   mainProperty_2 = "First item",  detailProperty_1="A", detailProperty_2 = "1" } ) ;
      Items.Add ( new ItemClass { mainProperty_1 = "Two",   mainProperty_2 = "Second item", detailProperty_1="B", detailProperty_2 = "2" } ) ;
      Items.Add ( new ItemClass { mainProperty_1 = "Three", mainProperty_2 = "Third item",  detailProperty_1="C", detailProperty_2 = "3" } ) ;
      Items.Add ( new ItemClass { mainProperty_1 = "Four",  mainProperty_2 = "Fourth item", detailProperty_1="D", detailProperty_2 = "4" } ) ;
      Items.Add ( new ItemClass { mainProperty_1 = "Five",  mainProperty_2 = "Fifth item",  detailProperty_1="E", detailProperty_2 = "5" } ) ;
      Items.Add ( new ItemClass { mainProperty_1 = "six",   mainProperty_2 = "Sixth item",  detailProperty_1="F", detailProperty_2 = "6" } ) ;
    }
  }
  </code></pre>
  
  <p>The main window just instantiates the model and sets it as the DataContext</p>
  
  <pre><code>public partial class MainWindow : Window
  {
    private Model   _model ;
  
    public MainWindow()
    {
      InitializeComponent();
  
      _model = new Model() ;
      this.DataContext = _model ;
    }
  }
  </code></pre>
  
  <p>The basic ListView is something like this:</p>
  
  <pre><code>&lt;ListView ItemsSource="{Binding Items}"&gt;
    &lt;ListView.View&gt;
      &lt;GridView&gt;
        &lt;GridViewColumn Width="100" Header="Main Property 1" DisplayMemberBinding="{Binding Path=mainProperty_1}"/&gt;
        &lt;GridViewColumn Width="200" Header="Main Property 2" DisplayMemberBinding="{Binding Path=mainProperty_2}"/&gt;
      &lt;/GridView&gt;
    &lt;/ListView.View&gt;
  &lt;/ListView&gt;
  </code></pre>
  
  <p>but this doesn't have any tooltips.</p>
  
  <p>I know that one way is to add something like:</p>
  
  <pre><code>&lt;ListView.ItemContainerStyle&gt;
    &lt;Style TargetType="{x:Type ListViewItem}"&gt;
      &lt;Setter Property="ToolTip" Value="{Binding Path=detailProperty_1}" /&gt;
    &lt;/Style&gt;
  &lt;/ListView.ItemContainerStyle&gt;
  </code></pre>
  
  <p>which shows a single property from the ItemClass, but I want something much more elaborate.</p>
  
  <p>I have defined a style resource containing a grid as a tooltip:</p>
  
  <pre><code>&lt;Window.Resources&gt;
  
    &lt;Style x:Key="ItemTooltip" TargetType="ToolTip"&gt;
      &lt;Setter Property="ContentTemplate"&gt;
        &lt;Setter.Value&gt;
          &lt;DataTemplate&gt;
  
            &lt;Grid Margin="5"&gt;
              &lt;Grid.RowDefinitions&gt;
                &lt;RowDefinition/&gt;
                &lt;RowDefinition/&gt;
                &lt;RowDefinition/&gt;
                &lt;RowDefinition/&gt;
              &lt;/Grid.RowDefinitions&gt;
              &lt;Grid.ColumnDefinitions&gt;
                &lt;ColumnDefinition Width="113"/&gt;
                &lt;ColumnDefinition Width="*"/&gt;
              &lt;/Grid.ColumnDefinitions&gt;
              &lt;TextBlock Grid.Row="0" Grid.Column="0"&gt;Main property 1:&lt;/TextBlock&gt;
              &lt;TextBlock Grid.Row="1" Grid.Column="0"&gt;Main property 2:&lt;/TextBlock&gt;
              &lt;TextBlock Grid.Row="2" Grid.Column="0"&gt;Detail property 1:&lt;/TextBlock&gt;
              &lt;TextBlock Grid.Row="3" Grid.Column="0"&gt;Detail property 2:&lt;/TextBlock&gt;
              &lt;TextBlock Grid.Row="0" Grid.Column="1" Text="{Binding Path=mainProperty_1}" /&gt;
              &lt;TextBlock Grid.Row="1" Grid.Column="1" Text="{Binding Path=mainProperty_2}" /&gt;
              &lt;TextBlock Grid.Row="2" Grid.Column="1" Text="{Binding Path=detailProperty_1}" /&gt;
              &lt;TextBlock Grid.Row="3" Grid.Column="1" Text="{Binding Path=detailProperty_2}" /&gt;
            &lt;/Grid&gt;
  
          &lt;/DataTemplate&gt;
        &lt;/Setter.Value&gt;
      &lt;/Setter&gt;
    &lt;/Style&gt;
  
  &lt;/Window.Resources&gt;
  </code></pre>
  
  <p>I have added a third column to the grid with a TextBlock, which uses this tooltip.</p>
  
  <pre><code>&lt;ListView ItemsSource="{Binding Items}"&gt;
    &lt;ListView.View&gt;
      &lt;GridView&gt;
        &lt;GridViewColumn Width="100" Header="Main Property 1" DisplayMemberBinding="{Binding Path=mainProperty_1}"/&gt;
        &lt;GridViewColumn Width="200" Header="Main Property 2" DisplayMemberBinding="{Binding Path=mainProperty_2}"/&gt;
  
        &lt;GridViewColumn Width="200" Header="Main Property 2 again" &gt;
          &lt;GridViewColumn.CellTemplate&gt;
            &lt;DataTemplate&gt;
              &lt;TextBlock Text="{Binding Path=mainProperty_2}"&gt;
                &lt;TextBlock.ToolTip&gt;
                  &lt;ToolTip Style="{StaticResource ItemTooltip}" DataContext="{Binding}" /&gt;
                &lt;/TextBlock.ToolTip&gt;
              &lt;/TextBlock&gt;
            &lt;/DataTemplate&gt;
          &lt;/GridViewColumn.CellTemplate&gt;
        &lt;/GridViewColumn&gt;
  
      &lt;/GridView&gt;
    &lt;/ListView.View&gt;
  &lt;/ListView&gt;
  </code></pre>
  
  <p>(I don't really want the TextBlock, but that is not the subject of this question.)</p>
  
  <p>The tooltip is displayed and shows the grid, but the bindings don't work, because I haven't set up the DataContext. I have defined four rows in the grid which should display fields from the ItemClass.</p>
  
  <p>How can I set up the DataContext in the tooltip, so that I can bind to properties of the item in the grid where the tooltip is shown?</p>