title: WPF, XAML, FindAncestor binding DataTemplate/ControlTemplate hierarchy
body: <p>We have the following XAML:</p>
  
  <pre><code>        &lt;DataTemplate x:Key="PictureMediaEditorInformationTemplate" DataType="mediaEditorInformation:PictureMediaEditorInformation"&gt;
          &lt;dxlc:LayoutGroup Orientation="Horizontal"&gt;
              &lt;dxlc:LayoutItem Label="{Binding LabelContent}"&gt;
                  &lt;dxlc:LayoutGroup Orientation="Vertical"&gt;
                      &lt;dxlc:LayoutItem Label="{Binding LabelTitle}" LabelPosition="Left"&gt;
                          &lt;dxe:TextEdit EditValue="{Binding Title, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/&gt;
                      &lt;/dxlc:LayoutItem&gt;
                      &lt;dxlc:LayoutItem&gt;
                          &lt;dxe:ImageEdit ShowLoadDialogOnClickMode="Empty" ShowBorder="true" AllowDrop="True" Drop="PictureEditor_OnDrop"
                      Source="{Binding VisibleImage, Converter={StaticResource LexImageToImageConverter}, 
                      Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Width="320" Height="320" ShowMenu="true"&gt;
                              &lt;dxmvvm:Interaction.Behaviors&gt;
                                  &lt;dxmvvm:EventToCommand EventName="MouseDoubleClick" Command="{Binding ViewPictureCommand}"/&gt;
                              &lt;/dxmvvm:Interaction.Behaviors&gt;
                              &lt;dxe:ImageEdit.MenuTemplate&gt;
                                  &lt;ControlTemplate&gt;
                                      &lt;StackPanel Orientation="Horizontal"&gt;
                                          &lt;dxe:ImageEditClearToolButton ImageSource="{Binding ButtonImage, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=dxlc:LayoutGroup}}"/&gt;
  </code></pre>
  
  <p>And we can't work out the correct way to specify the DataTemplate datacontext for the ImageSource binding. It might be complicated by the fact that the DataTemplate is a resource instantiated in code using:</p>
  
  <pre><code>var template = (DataTemplate) lg.FindResource(item.DataTemplateName);
  
              var uiElement = template.LoadContent() as FrameworkElement;
  </code></pre>
  
  <p>We've tried various things but with no success so far. I had hoped that setting uiElement.Name could be used in conjunction with ElementName but it fails presumably because it's crossing from a ControlTemplate to a DataTemplate.</p>
  
  <p>NB:In the actual code the property is not called 'Wibble' :)</p>