title: Managing keyboard focus with custom TextBox control template
body: <p>Currently I'm working on a WPF project which has defined a custom style for <code>TextBox</code>.  Among other things, we override the default control template for the <code>TextBox</code>.  The control template looks like this (I've removed a lot of the <code>TemplateBinding</code> stuff since it's not relevant).</p>
  
  <pre><code>&lt;Border&gt;
      &lt;Grid&gt;
          &lt;Grid.ColumnDefinitions&gt;
              &lt;ColumnDefinition Width="*"/&gt;
              &lt;ColumnDefinition Width="Auto"/&gt;
          &lt;/Grid.ColumnDefinitions&gt;
          &lt;ScrollViewer x:Name="PART_ContentHost"
                        IsTabStop="False"
                        Background="{x:Null}"
                        VerticalAlignment="Center"
                        BorderThickness="0"/&gt;
  
          &lt;ContentPresenter Grid.Column="1" Margin="5,0,0,0" Focusable="False"
                            Content="{TemplateBinding local:UnitConversion.Info}"
                            ContentTemplateSelector="{DynamicResource SelectorKey}"/&gt;
      &lt;/Grid&gt;
  &lt;/Border&gt;
  </code></pre>
  
  <p>Essentially this just adds a <code>ContentPresenter</code> to display a combo box allowing the user to select different units to be used for display or entry into the text box.  The template selector simply selects between the user control (which is really just a <code>ComboBox</code>) or an empty template if the text box doesn't have any associated unit data (the content is <code>null</code>).</p>
  
  <h3>The Problem</h3>
  
  <p>Since this combo box is essentially part of the text box control template, interacting with the combo box gives keyboard focus to the text box without actually placing the caret into the visual text box.  Elsewhere we have a keyboard control which is ultimately made visible when a text box gets keyboard focus (the keyboard itself is actually listening for custom routed events, but these are ultimately raised when a <code>TextBox</code> gets focus.</p>
  
  <h3>The Question</h3>
  
  <p>Is there any way I can prevent the text box from getting focus when anything other than the actual text entry is what technically got focus?  My desired behavior is that manipulating the combo box doesn't result in the on-screen keyboard popping up.</p>
  
  <p>My gut instinct here tells me that I can't actually do what I want since all the focus logic happens at the <code>TextBox</code> level and it doesn't really care much about the contents of the control template.  But I'm just curious if anyone might be able to think of some kind of workaround.</p>
  
  <p>I'd like to avoid introducing a custom text box control to handle this since we don't much like the idea of application-specific custom controls (especially considering how far we've gotten with the current one). </p>