title: Keyboard.Focus does not work on text box in WPF
body: <p>I am banging my head on what looks like such a simple problem to fix in wpf but i have yet to discover why i can't get my app to behave according to my plan. </p>
  
  <p>I have a small search box that pops-up in my wpf application when user presses ctrl+f. All i want is for the caret to be flashing inside the search box text box, ready to take whatever user input without the user having to click on it. Here is the xaml code for the text box which is visible, enabled, hit testable, tabstopable and focusable.</p>
  
  <pre><code>   &lt;TextBox x:Name="SearchCriteriaTextBox" Text="{Binding SearchCriteria}" Focusable="True" IsEnabled="True" IsTabStop="True" IsHitTestVisible="True" Style="{DynamicResource SearchTextBoxStyle}" Grid.Column="1" Margin="5,10,0,5" /&gt;
  </code></pre>
  
  <p>In the code behind, i have this method called when the visibility of the search box is affected. the search box is loaded at the start of the app.</p>
  
  <pre><code>    /// &lt;summary&gt;
      /// Handles events triggered from focusing on this view.
      /// &lt;/summary&gt;
      /// &lt;param name="sender"&gt;The sender.&lt;/param&gt;
      /// &lt;param name="dependencyPropertyChangedEventArgs"&gt;The key event args.&lt;/param&gt;
      private void OnIsVisibleChanged(object sender, DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs)
      {
          if (!((bool) dependencyPropertyChangedEventArgs.NewValue))
          {
              return;
          }
  
          SearchCriteriaTextBox.Focus();
          Keyboard.Focus(SearchCriteriaTextBox);
          SearchCriteriaTextBox.Select(0, 0);
  
          if (SearchCriteriaTextBox.Text.Length &gt; 0)
          {
              SearchCriteriaTextBox.SelectAll();
          }
      }
  </code></pre>
  
  <p>The problem is, code gets called, component becomes IsFocused=true but does not gain keyboard focus. 
  Am I missing something? Unless another control keeps hold ferociously to the keyboard focus which i am pretty sure i didn't code, why would this piece of rather simple code would not work properly.</p>