title: Set textbox focus after button press
body: <p>I have the following textbox in a user control.</p>
  
  <pre><code>&lt;!-- This is the user input TextBox, users type commands here and hit enter or press the send command button --&gt;
  &lt;TextBox Text="{Binding CommandText}" Background="Transparent" BorderBrush="{StaticResource brushWatermarkBorder}" Name="txtUserEntry"&gt;
      &lt;TextBox.InputBindings&gt;
          &lt;KeyBinding Command="{Binding BindKeyCommand}"
                  CommandParameter="{Binding ElementName=txtUserEntry, Path=Text}"
                  Key="Return"
                  Modifiers=""/&gt;
      &lt;/TextBox.InputBindings&gt;
  &lt;/TextBox&gt;
  </code></pre>
  
  <p>and button.</p>
  
  <pre><code>&lt;!-- This button serves as an alternative to hitting the enter key with text box focus. --&gt;
  &lt;Button Command="{Binding BindKeyCommand}"  CommandParameter="{Binding ElementName=txtUserEntry, Path=Text}" Grid.Column="2"&gt;
      &lt;TextBlock&gt;
                  Send Command
      &lt;/TextBlock&gt;
  &lt;/Button&gt;
  </code></pre>
  
  <p>When a user types into the box and uses the return key too send the command, the command is sent through the BindKeyCommand successfully (a messagebox pops up to confirm this) and focus is maintained on the text box. </p>
  
  <p>However, when a command is sent using the Button, the command is, once again, successful, but the focus on the text box is lost.</p>
  
  <p>I have attempted to implement the answers <a href="https://stackoverflow.com/questions/1356045/set-focus-on-textbox-in-wpf-from-view-model-c">here</a> but all to no avail. Could someone please explain to me, either, how to correctly implement the answers here, including what to put in the view model, or, an alternative method such that, on button press, the keyboard focus is assigned back to the textbox. It might be important to note, just as in the linked question, I am using MVVM.</p>
  
  <p>UPDATE: Trying to carry out steps by DT Sawant</p>
  
  <p>Step 1:</p>
  
  <p>ApplicationName.Tools.FocusExtension contains all of the text within second highest rated answer. Extension is in namespace ApplicationName.Tools</p>
  
  <p>Step 2:</p>
  
  <p>Added new property;</p>
  
  <pre><code>private bool isTxtUserEntryFocused = false;
  public bool IsTxtUserEntryFocused
  {
      get
      {
         return isTxtUserEntryFocused;
      }
          set
          {
              isTxtUserEntryFocused = value;
              OnPropertyChanged("IsTxtUserEntryFocused");
          }
      }
  </code></pre>
  
  <p>Step 3:</p>
  
  <p>Added namespace reference; xmlns:Tools ="clr-namespace:WPFLocalDataConnect.Tools"</p>
  
  <p>Step 4:</p>
  
  <p>Bound IsFocused property; Tools:FocusExtension.IsFocused="{Binding IsTxtUserEntryFocused}"</p>
  
  <p>Step 5:</p>
  
  <p>Initializing in viewmodel thusly;</p>
  
  <pre><code>        public void ExecuteBindKeyCommand(string param)
      {
          if (String.IsNullOrWhiteSpace(param))
          {
              MessageBox.Show("No command given.");
              this.CommandText = string.Format("");
              IsTxtUserEntryFocused = true;
          }
          else
          {
              MessageBox.Show(string.Format("CommandInvoked: {0}", param));
              History = string.Format("{0} {2} {1}", History, param, Environment.NewLine);
              this.CommandText = string.Format("");
              IsTxtUserEntryFocused = true;
          }
      }
  </code></pre>
  
  <p>Now all steps are completed I tested the project. It did not work but some interesting things to note;</p>
  
  <p>Pressing the button WITHOUT first focusing the textbox results in "no command" message followed by focus move to textbox. Pressing the button after clicking in  the textbox, either entering or not entering text results in "no command" or "command invoked" messages respectively followed by no focus move to the textbox. THIS IS SO CONFUSING.</p>