title: Add element to control template in code
body: <p>Let's say I have a common <code>UserControl</code> that has it's default <code>ControlTemplate</code>. Now I want to add an <code>AdornerDecorator</code> as the root element of the <code>ControlTemplate</code>.</p>
  
  <p>How would I do that?</p>
  
  <p>My attempt so far is the following:</p>
  
  <pre><code>UserControl view;
  var templateWithAdornerDecorator = new ControlTemplate(view.GetType());
  var adornerDecorator = new FrameworkElementFactory(typeof(AdornerDecorator));
  var otherView = new FrameworkElementFactory(view.GetType());
  otherView.SetValue(Control.TemplateProperty, view.Template);
  adornerDecorator.AppendChild(otherView);
  templateWithAdornerDecorator.VisualTree = adornerDecorator;
  view.Template = templateWithAdornerDecorator;
  </code></pre>
  
  <p>(My <code>view</code> might be of a derived type of <code>UserControl</code>.)
  But that crashes with a StackOverflowException :(</p>
  
  <p><strong>EDIT:</strong>
  <code>MyUserControl</code> has this xaml:</p>
  
  <pre><code>&lt;UserControl x:Class="MyNameSpace.Wpf.Sample.MyUserControl"
               xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
               xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
               xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
               xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
               mc:Ignorable="d" 
               d:DesignHeight="300" d:DesignWidth="300"&gt;
      ABA
  &lt;/UserControl&gt;
  </code></pre>
  
  <p>and this is the xaml.cs:</p>
  
  <pre><code>public partial class MyUserControl : UserControl
  {
      public MyUserControl()
      {
          InitializeComponent();
      }
  }
  </code></pre>