title: How to set the keyboardfocus to a textbox inside a UserControl programmatically?
body: <p>I have a UserControl that incorporates a textbox. I want to set the keyboardfocus to this textbox programmatically when the user clicks a button.
  I tried this:</p>
  
  <pre><code>private void Button_Click(object sender,EventArgs e)
  {
    Keyboard.Focus(MyUserControl);
  }
  </code></pre>
  
  <p>no luck. Then I exposed the Textbox in the UserControl though a property of type TextBox</p>
  
  <pre><code>private void Button_Click(object sender,EventArgs e)
  {
    Keyboard.Focus(MyUserControl.TextBox);
  }
  </code></pre>
  
  <p>again no luck. Finally I made an eventhandler in the UserControl to handle the GotKeyboardFocus event, calling the Keyboard.Focus method on the textbox inside it.</p>
  
  <p>Again no luck?!</p>
  
  <p>How to do this??</p>
  
  <p>EDIT:
  The problem is not related to UserControls. It is an issue when you try to pass focus to an other UIElement in a Click or MouseDownHandler.
  The XAML en code behind below tell their own story: the focus does pass to the textbox but is stolen back by the listbox.</p>
  
  <pre><code>&lt;Window x:Class="FocusSpike.Window1"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      Title="Window1" Height="300" Width="300"&gt;
      &lt;StackPanel&gt;
          &lt;TextBox Name="FocusedTextBox" Height="30"&gt;
              &lt;TextBox.Style&gt;
                  &lt;Style TargetType="TextBox"&gt;
                      &lt;Setter Property="Text" Value="I am unfocused..."/&gt;
                      &lt;Setter Property="Opacity" Value=".3"/&gt;
                      &lt;Style.Triggers&gt;
                          &lt;Trigger Property="IsKeyboardFocused" Value="True"&gt;
                              &lt;Trigger.EnterActions&gt;
                                  &lt;BeginStoryboard&gt;
                                      &lt;Storyboard AutoReverse="True"&gt;
                                          &lt;DoubleAnimation Storyboard.TargetProperty="FontSize" To="20"/&gt;
                                      &lt;/Storyboard&gt;
                                  &lt;/BeginStoryboard&gt;
                              &lt;/Trigger.EnterActions&gt;
                              &lt;Setter Property="Text" Value="I am focused!"/&gt;
                              &lt;Setter Property="Opacity" Value="1"/&gt;
                          &lt;/Trigger&gt;
                      &lt;/Style.Triggers&gt;
                  &lt;/Style&gt;
              &lt;/TextBox.Style&gt;
          &lt;/TextBox&gt;
          &lt;Button&gt;Click to steal focus.&lt;/Button&gt;
          &lt;ListBox&gt;
              &lt;ListBoxItem GotFocus="Listbox_GotFocus"&gt;
                  &lt;Label MouseDown="ListBoxItem_MouseDown"&gt;
                      Click to restore focus
                  &lt;/Label&gt;
              &lt;/ListBoxItem&gt;
          &lt;/ListBox&gt;
      &lt;/StackPanel&gt;
  &lt;/Window&gt;
  
  using System.Windows;
  using System.Windows.Input;
  namespace FocusSpike
  {
      public partial class Window1 : Window
      {
          public Window1()
          {
              InitializeComponent();
              FocusedTextBox.Focus();
          }
  
          private void ListBoxItem_MouseDown(object sender, MouseButtonEventArgs e)
          {
              Keyboard.Focus(FocusedTextBox);//This does not work, remove it!
          }
  
          private void Listbox_GotFocus(object sender, RoutedEventArgs e)
          {
              //Keyboard.Focus(FocusedTextBox);//uncomment to restore focus!
          }
      }
  }
  </code></pre>