title: Bind to parent control from within context menu within data template
body: <p>I'm wanting to bind to a parent control from a context menu within a datatemplate. </p>
  
  <p>Unfortunately I'm restricted to .net 3.5 and can't use the x:reference extension introduced in .net 4.</p>
  
  <p>Below is an example of what I'm trying to do</p>
  
  <pre><code>&lt;Window x:Class="WpfApplication17.Window1"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      xmlns:local="clr-namespace:WpfApplication17"
      Name="window"&gt;
  
      &lt;Window.Resources&gt;
          &lt;DataTemplate DataType="{x:Type local:Car}"&gt;
              &lt;Rectangle Width="100" Height="100" Fill="Red"&gt;
                  &lt;Rectangle.ContextMenu&gt;
                      &lt;ContextMenu&gt;
                          &lt;MenuItem Header="{Binding Colour}"/&gt;
                          &lt;MenuItem Header="{Binding ElementName=window, Path=ActualWidth}"/&gt;
                      &lt;/ContextMenu&gt;
                  &lt;/Rectangle.ContextMenu&gt;
              &lt;/Rectangle&gt;
          &lt;/DataTemplate&gt;
      &lt;/Window.Resources&gt;
  &lt;/Window&gt;
  </code></pre>
  
  <p>But I get "Cannot find source for binding with reference 'ElementName=window'" error due to the context menu not being part of the visual tree.</p>
  
  <p>Edit :</p>
  
  <p>That works great! .. however, it doesn't seem to work when I use a composite collection such as the following</p>
  
  <pre><code>&lt;Window.Resources&gt;
          &lt;DataTemplate DataType="{x:Type local:Car}"&gt;
              &lt;Rectangle Width="100" Height="100" Fill="Red"
                 Tag="{Binding RelativeSource={RelativeSource AncestorType={x:Type Window}}}"&gt;
                  &lt;Rectangle.ContextMenu&gt;
                      &lt;ContextMenu&gt;
                          &lt;ContextMenu.ItemsSource&gt;
                              &lt;CompositeCollection&gt;
                                  &lt;MenuItem Header="{Binding Colour}"/&gt;
                                  &lt;MenuItem Header="{Binding Path=PlacementTarget.Tag.ActualWidth, RelativeSource={RelativeSource AncestorType=ContextMenu}}"/&gt;
                              &lt;/CompositeCollection&gt;
                          &lt;/ContextMenu.ItemsSource&gt;
                      &lt;/ContextMenu&gt;
  
                      &lt;!--&lt;ContextMenu&gt;
                          &lt;MenuItem Header="{Binding Colour}"/&gt;
                          &lt;MenuItem Header="{Binding Path=PlacementTarget.Tag.ActualWidth, RelativeSource={RelativeSource AncestorType=ContextMenu}}"/&gt;
                      &lt;/ContextMenu&gt;--&gt;
  
                  &lt;/Rectangle.ContextMenu&gt;
              &lt;/Rectangle&gt;
          &lt;/DataTemplate&gt;
      &lt;/Window.Resources&gt;
  </code></pre>