title: WPF and MVVM : How to move focus to the next Control automatically
body: <p>I have a little WPF Window with 2 TextBoxes Having Ordered TabIndex 0,1 and i want to move focus automatically from the first TextBox to the second  when i press Enter Key. 
  I Use MVVM Light.</p>
  
  <p>Remark : This post is not duplicated. here I do not use Classic approach with event Handler but MVVM Pattern and as you know Code Behind is not allowed in view.</p>
  
  <p>I Found a solution but I don't know if it respect MVVM Principle. 
   Here the code :</p>
  
  <p>The View</p>
  
  <pre><code>&lt;Window x:Class="WpfMMVLight2.MainWindow"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
      xmlns:cmd ="http://www.galasoft.ch/mvvmlight"
      Title="MainWindow" Height="350" Width="525"
      DataContext="{Binding MainViewModel}"&gt;
      &lt;Grid FocusManager.FocusedElement="{Binding ElementName=tb1}"&gt;
      &lt;Grid.ColumnDefinitions&gt;
          &lt;ColumnDefinition Width="Auto"/&gt;
          &lt;ColumnDefinition Width="70"/&gt;
      &lt;/Grid.ColumnDefinitions&gt;
  
      &lt;Grid.RowDefinitions&gt;
          &lt;RowDefinition Height="Auto"/&gt;
          &lt;RowDefinition Height="Auto"/&gt;
      &lt;/Grid.RowDefinitions&gt;
  
      &lt;TextBlock Grid.Column="0" Text="Text 1" VerticalAlignment="Center"/&gt;
      &lt;TextBox x:Name="tb1" Grid.Column="1" VerticalAlignment="Center" Margin="5"&gt;
          &lt;i:Interaction.Triggers&gt;
              &lt;i:EventTrigger EventName="KeyDown"&gt;
                  &lt;cmd:EventToCommand PassEventArgsToCommand="True"
                  Command ="{Binding KeyDownCommand, Mode=OneWay}"/&gt;
              &lt;/i:EventTrigger&gt;
          &lt;/i:Interaction.Triggers&gt;
      &lt;/TextBox&gt;
  
  
       &lt;TextBlock Grid.Column="0" Grid.Row="1" Text="Text 2" VerticalAlignment="Center"/&gt;
       &lt;TextBox x:Name="tb2" Grid.Column="1" Grid.Row="1" VerticalAlignment="Center" Margin="5"&gt;
          &lt;i:Interaction.Triggers&gt;
              &lt;i:EventTrigger EventName="KeyDown"&gt;
                  &lt;cmd:EventToCommand PassEventArgsToCommand="True"
              Command ="{Binding KeyDownCommand, Mode=OneWay}"/&gt;
              &lt;/i:EventTrigger&gt;
          &lt;/i:Interaction.Triggers&gt;
      &lt;/TextBox&gt;
  
  &lt;/Grid&gt;
  </code></pre>
  
  <p></p>
  
  <p>The ViewModel :</p>
  
  <pre><code> private ICommand _keydownCommand;
   public ICommand KeyDownCommand
      {
         get
          {
              if (_keydownCommand== null)
                  _keydownCommand= new DelegateCommand&lt;KeyEventArgs&gt;(KeyDownCommandExecute);
              return _keydownCommand;
          }
  
  
  
      }
  
  
      private void KeyDownCommandExecute(KeyEventArgs e)
      {
          if (e != null &amp;&amp; e.Key == Key.Enter)
          {
        TraversalRequest request = new TraversalRequest(FocusNavigationDirection.Next);
        request.Wrapped = true;
        ((Control)e.Source).MoveFocus(request);
       }
      }
  }    
  </code></pre>
  
  <p>I don't know if use of "Control" Class in ViewModel Is allowed or not</p>