title: Setting Focus on a Control Within a ControlTemplate (Part 2)
body: <p>I'm stumped on what must surely be one of the most common WPF requirements.  I've read <a href="https://stackoverflow.com/questions/158536/setting-focus-on-a-control-within-a-controltemplate-in-wpf/" title="this">this question</a> but my implementation of the solution does not work.</p>
  
  <p>Here's the markup for the lookless control:</p>
  
  <pre><code>&lt;ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                  xmlns:local="clr-namespace:WpfTest"&gt;
    &lt;Style TargetType="{x:Type local:CustomControl}"&gt;
      &lt;Setter Property="Template"&gt;
        &lt;Setter.Value&gt;
          &lt;ControlTemplate TargetType="{x:Type local:CustomControl}"&gt;
            &lt;Border&gt;
              &lt;TextBox x:Name="myTextBox" /&gt;
            &lt;/Border&gt;
            &lt;ControlTemplate.Triggers&gt;
              &lt;Trigger Property="IsFocused"
                       Value="True"&gt;
                &lt;Setter Property="FocusManager.FocusedElement"
                        Value="{Binding ElementName=myTextBox}" /&gt;
                &lt;Setter TargetName="myTextBox"
                        Property="Background"
                        Value="Green" /&gt;
              &lt;/Trigger&gt;
            &lt;/ControlTemplate.Triggers&gt;
          &lt;/ControlTemplate&gt;
        &lt;/Setter.Value&gt;
      &lt;/Setter&gt;
    &lt;/Style&gt;
  &lt;/ResourceDictionary&gt;
  </code></pre>
  
  <p>Here's the markup for the Window that contains an instance of the CustomControl:</p>
  
  <pre><code>&lt;Window x:Class="WpfTest.Window1"
          xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
          xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
          xmlns:local="clr-namespace:WpfTest"
          Title="Window1" Height="300" Width="300"&gt;
  
    &lt;local:CustomControl x:Name="CCtl" /&gt;
  &lt;/Window&gt;
  </code></pre>
  
  <p>And here's the code-behind:</p>
  
  <pre><code>public partial class Window1 : Window
  {
      public Window1()
      {
          InitializeComponent();
          Loaded += (RoutedEventHandler)delegate { CCtl.Focus(); };
      }
  }
  </code></pre>
  
  <p>When Window1 is loaded, the text box turns green (indicating that the trigger works) but focus remains with CCtl and not the text box.  Doubtless this has to do with the output displaying the following data error:</p>
  
  <blockquote>
    <p>Cannot find source for binding with reference 'ElementName=myTextBox'.
    BindingExpression:(no path); DataItem=null; target element is
    'CustomControl' (Name='CCtl'); target property is 'FocusedElement'
    (type 'IInputElement').</p>
  </blockquote>
  
  <p>I've no idea why this error is appearing.  Any pointers gratefully received, thanks.</p>