title: "Tabbing" between fields using TabIndex
body: <h1>PROBLEM</h1>
  
  <p>In my <strong>Windows Phone 8 C#/XAML Application using .NET 4.5</strong> I'm trying to <em>"iterate"</em> through form. In other words, when the user presses <em>"Enter"</em> key, the focus changes to another <code>TextBox</code> or <code>PasswordBox</code>.</p>
  
  <p><strong>HOW MY CODE LOOKS LIKE NOW</strong></p>
  
  <p>XAML:</p>
  
  <pre><code>&lt;TextBox TabIndex="0" KeyDown="TextBox_KeyDown"/&gt;
  &lt;TextBox TabIndex="1" KeyDown="TextBox_KeyDown"/&gt;
  &lt;TextBox TabIndex="2" KeyDown="TextBox_KeyDown"/&gt;
  &lt;TextBox TabIndex="3" KeyDown="TextBox_KeyDown"/&gt;
  &lt;TextBox TabIndex="4" KeyDown="TextBox_KeyDown"/&gt;
  ....
  </code></pre>
  
  <p>C# CodeBehind:</p>
  
  <pre><code>using System.Windows.Input;
  
  private void TextBox_KeyDown(object sender, System.Windows.Input.KeyEventArgs e)
  {
      if (e.Key.Equals(Key.Enter))
      {
          int index = ((TextBox)sender).TabIndex + 1;
          //and here is what I've been missing
          //basically the code to select next tabindex
          //and set focus on it using Focus() method...
      }
  }    
  </code></pre>
  
  <h1>QUESTION</h1>
  
  <ol>
  <li><strong>HOW TO SELECT NEXT ELEMENT TO FOCUS ON BY TABINDEX???</strong> I know I should probably use LINQ, to select all textboxes in the usercontrol and then select the one with tabindex index. I'm slowly figuring it on my own (and also by discussing it with people in the comments), but as it's taking me a long time I wrote the question :)</li>
  </ol>
  
  <p>*(also, I was not sure how to name the question, feel free to rename it if you think your name fits it better)</p>
  
  <h1>SOLUTION BASED ON SACHA's ANSWER</h1>
  
  <p><strong>LayoutRoot is the main grid of usercontrol</strong></p>
  
  <pre><code>    private void TextBox_KeyDown(object sender, System.Windows.Input.KeyEventArgs e)
      {
          if (e.Key.Equals(Key.Enter))
          {
              int index = ((TextBox)sender).TabIndex + 1;
              var nextBox = LayoutRoot.Children.OfType&lt;TextBox&gt;().FirstOrDefault((x) =&gt; { return x.TabIndex == index; });
  
              if (nextBox != null)
              {
                  nextBox.Focus();
              }
          }
      }
  </code></pre>
  
  <p>Thanks a lot! :)</p>