title: WPF datagrid, autocompletebox cell
body: <p>I have a datagrid in which a AutoCompleteBox
  I want when I switch the focus to the cell containing the ACB, it goes directly to input mode. Currently I have to press the enter key to make it. 
  here the code :</p>
  
  <pre><code>   &lt;DataGrid Grid.ColumnSpan="3"
                  AutoGenerateColumns="False" 
                  HorizontalAlignment="Stretch" 
                  VerticalAlignment="Stretch"
                  IsSynchronizedWithCurrentItem="True"
                  RowHeight="30"
                  Grid.Column="0" 
                  SelectionUnit="Cell"
                  ItemsSource="{Binding  RowsToEdit, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" 
  
                  SelectedItem="{Binding SelectedPerson, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"
                  b:DataGridEnterKeyBehavior.EnterKeyNexCellProperty="True"
                  b:DataGridEnterEditOnCellGotFocus.EnterEditOnGotFocusProperty="True" Margin="0,45,0,5" Grid.RowSpan="2"
  
                &gt;
  
  
          &lt;DataGrid.Columns&gt;
              &lt;DataGridTextColumn Binding="{Binding Code, UpdateSourceTrigger=PropertyChanged,  Mode=TwoWay, StringFormat=\{0:#\}}"  Header="Code" &gt;
                  &lt;i:Interaction.Triggers&gt;
                      &lt;i:EventTrigger EventName="PreviewKeyDown"&gt;
                          &lt;gs:EventToCommand Command="{Binding HandleCellKeyDownCommand, Source={StaticResource MyViewModel}}"
                                             PassEventArgsToCommand="True"
  
                                             /&gt;
                      &lt;/i:EventTrigger&gt;
                  &lt;/i:Interaction.Triggers&gt;
              &lt;/DataGridTextColumn&gt;
              &lt;DataGridTemplateColumn Header="Name" &gt;
  
                  &lt;DataGridTemplateColumn.CellTemplate&gt;
                      &lt;DataTemplate&gt;
                          &lt;TextBlock Text="{Binding Nom}"/&gt;
                      &lt;/DataTemplate&gt;
                  &lt;/DataGridTemplateColumn.CellTemplate&gt;
                  &lt;!--FocusManager.FocusedElement="{Binding RelativeSource={RelativeSource Self}}" --&gt;
                  &lt;DataGridTemplateColumn.CellEditingTemplate&gt;
                      &lt;DataTemplate&gt;
  
                          &lt;controls:AutoCompleteBox
                                  x:Name="acb2"
                                  Text="{Binding Nom, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" 
                                  ItemsSource="{Binding Names,Source={StaticResource MyViewModel}}" 
                                  SelectedItem="{Binding Path=SelectedPerson, Source={StaticResource MyViewModel}, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"
                                  ValueMemberBinding="{Binding Nom}"
                                  Style="{StaticResource acbStyle}"
                                  ItemTemplate="{StaticResource AutoCompleteBoxItemTemplate}"
  
                                  &gt;
                              &lt;i:Interaction.Triggers&gt;
                                  &lt;i:EventTrigger EventName="SelectionChanged"&gt;
                                      &lt;gs:EventToCommand Command="{Binding SelectionChangedCommand, Source={StaticResource MyViewModel}}" 
                                             CommandParameter="{Binding Path=SelectedPerson, Source={StaticResource MyViewModel}}"
                                             /&gt;
                                  &lt;/i:EventTrigger&gt;
                              &lt;/i:Interaction.Triggers&gt;
                          &lt;/controls:AutoCompleteBox&gt;
                      &lt;/DataTemplate&gt;
                  &lt;/DataGridTemplateColumn.CellEditingTemplate&gt;
  
              &lt;/DataGridTemplateColumn&gt;
  
              &lt;DataGridTextColumn Binding="{Binding Adresse, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"       Header="Adresse" /&gt;
  
          &lt;/DataGrid.Columns&gt;
      &lt;/DataGrid&gt;
  
  
  
  
  //Behaviors
      //Behavior for passing automatically in edit mode when cell get focus
    public static class DataGridEnterEditOnCellGotFocus
  {
  
  
      public static readonly DependencyProperty EnterEditOnGotFocusProperty =
       DependencyProperty.RegisterAttached("EnterEditOnGotFocus",
       typeof(bool),
       typeof(DataGridEnterEditOnCellGotFocus),
       new UIPropertyMetadata(false, ProcessCellGotFocusEnter));
  
  
  
      public static bool GetEnterEditOnGotFocusProperty(DependencyObject obj)
      {
          return (bool)obj.GetValue(EnterEditOnGotFocusProperty);
      }
  
      public static void SetEnterEditOnGotFocusProperty(DependencyObject obj, bool value)
      {
          obj.SetValue(EnterEditOnGotFocusProperty, value);
      }
  
  
  
      private static void ProcessCellGotFocusEnter(DependencyObject d, DependencyPropertyChangedEventArgs e)
      {
          DataGrid dg = (d as DataGrid);
  
          if (dg != null)
          {
              if ((bool)e.NewValue)
                  dg.GotFocus += dg_GotFocus;
              else
                  dg.GotFocus -= dg_GotFocus;
          }
      }
  
  
      private static void dg_GotFocus(object sender, RoutedEventArgs e)
      {
  
  
          if (e.OriginalSource.GetType() == typeof(DataGridCell))
          {
  
              DataGridCell source = e.OriginalSource as DataGridCell;
  
              DataGrid grd = (DataGrid)sender;
              grd.BeginEdit(e);
  
              Control control = GetFirstChildByType&lt;Control&gt;(source);
              if (control != null)
              {
  
                  control.Focus();
  
               }
          }
      }
  
  //Behaviors for moving from cell to another with entre key
  public static class DataGridEnterKeyBehavior
  {
  
  
      public static readonly DependencyProperty EnterKeyNexCellProperty =
       DependencyProperty.RegisterAttached("EnterKeyNexCell",
       typeof(bool),
       typeof(DataGridEnterKeyBehavior),
       new UIPropertyMetadata(false, ProcessEnterKeyDown));
  
  
  
      public static bool GetEnterKeyNexCellProperty(DependencyObject obj)
      {
          return (bool)obj.GetValue(EnterKeyNexCellProperty);
      }
  
      public static void SetEnterKeyNexCellProperty(DependencyObject obj, bool value)
      {
          obj.SetValue(EnterKeyNexCellProperty, value);
      }
  
  
  
      private static void ProcessEnterKeyDown(DependencyObject d, DependencyPropertyChangedEventArgs e)
      {
          DataGrid dg = (d as DataGrid);
  
          if (dg != null)
          {
              if ((bool)e.NewValue)
                  dg.PreviewKeyDown += dg_PreviewKeyDownDatagrid;
              else
                  dg.PreviewKeyDown -= dg_PreviewKeyDownDatagrid;
          }
      }
  
  
      private static void dg_PreviewKeyDownDatagrid(object sender, KeyEventArgs e)
      {
  
          DataGrid dg = (sender as DataGrid);
  
          var uiElement = e.OriginalSource as UIElement;
          if (e.Key == Key.Enter &amp;&amp; uiElement != null)
          {
              e.Handled = true;
              dg.SelectedCells.Clear();
              uiElement.MoveFocus(new TraversalRequest(FocusNavigationDirection.Next));
              dg.BeginEdit();
  
          }
  
      }
  }
  </code></pre>
  
  <p>And the ViewModel</p>
  
  <pre><code>public class MyViewModel : ObservableObject
  {
      ObservableCollection&lt;Person&gt; _names = null;
      ObservableCollection&lt;Person&gt; _rowsToEdit = null;
  
      public ObservableCollection&lt;Person&gt; RowsToEdit
      {
          get { return _rowsToEdit; }
          set
          {
              _rowsToEdit = value;
              RaisePropertyChanged("RowsToEdit");
          }
      }
  
      RelayCommand _loadClients;
      RelayCommand _showSelectedPerson;
      RelayCommand&lt;Person&gt; _selectchangedcommand;
      RelayCommand&lt;KeyEventArgs&gt; _handleCellKeyDown;
  
      Person _selectedPerson;
  
  
  
  
      public Person SelectedPerson
      {
          get { return _selectedPerson; }
          set
          {
              _selectedPerson = value;
              RaisePropertyChanged("SelectedPerson");
          }
      }
  
  
  
      public ObservableCollection&lt;Person&gt; Names
      {
          get { return _names; }
          set
          {
              _names = value;
              RaisePropertyChanged("Names");
          }
      }
  
  
  
  
      public RelayCommand LoadClientCommand
      {
          get
          {
              if (_loadClients == null)
                  _loadClients = new RelayCommand(LoadCommandExecute);
              return _loadClients;
          }
      }
  
  
  
      private void LoadCommandExecute()
      {
          LoadClients();
          List&lt;Person&gt; ll = new List&lt;Person&gt;(1);
          ll.Add(new Person(0, "", ""));
          RowsToEdit = new ObservableCollection&lt;Person&gt;(ll);
      }
  
  
  
      public void LoadClients()
      {
          List&lt;Person&gt; ll = new List&lt;Person&gt;(8);
          ll.Add(new Person(1, "AAAAAAA", "Adresse1"));
          ll.Add(new Person(2, "AABBBBB", "Adresse 2"));
          ll.Add(new Person(3, "AAACCCC", "Adresse3"));
          ll.Add(new Person(4, "BBBBHHHH", "Adresse4"));
          ll.Add(new Person(5, "BBBCGGFR", "Adresse5"));
          ll.Add(new Person(6, "JJJJJHHGYGH", "Adresse6"));
          ll.Add(new Person(7, "HHHHHHHHGGG", "Adresse7"));
          ll.Add(new Person(8, "AAAAHHGBV", "Adresse8"));
  
          Names = new ObservableCollection&lt;Person&gt;(ll);
  
      }
  
  
  
      public RelayCommand ShowSelectedPersonCommand
      {
          get
          {
              if (_showSelectedPerson == null)
                  _showSelectedPerson = new RelayCommand(ShowSelectedPersonCommandExecute);
              return _showSelectedPerson;
          }
      }
  
  
  
      private void ShowSelectedPersonCommandExecute()
      {
          if (SelectedPerson != null)
              MessageBox.Show(SelectedPerson.Nom);
          else
              MessageBox.Show("No sslections.");
  
      }
  
  
      public RelayCommand&lt;Person&gt; SelectionChangedCommand
      {
          get
          {
              if (_selectchangedcommand == null)
                  _selectchangedcommand = new RelayCommand&lt;Person&gt;(SelectionChangedCommandExecute);
              return _selectchangedcommand;
          }
      }
  
  
      private void SelectionChangedCommandExecute(Person SelectedPerson)
      {
          /*if (SelectedPerson != null)
              MessageBox.Show(SelectedPerson.Code.ToString());
          else
              MessageBox.Show("Pas de sélection.");
          */
  
      }
  
  
    }
  </code></pre>