title: How to prevent DataTemplate textbox losing focus when item is added to Itemscontrol?
body: <p>I have an ItemsControl with ItemsSource bound to an IEnumerable&lt;MyDataItem>.</p>
  
  <p>The ItemTemplate consists of two textboxes. (Friendly name &amp; name). This is how it looks like: <a href="http://i.stack.imgur.com/Rg1dC.png" rel="nofollow">http://i.stack.imgur.com/Rg1dC.png</a></p>
  
  <p>As soon as the "Friendly name" field is filled in, I add an empty row. I use the LostKeyboardFocus event to check whether to add an empty "MyDataItem" and refresh the IEnumerable&lt;MyDataItem> property.</p>
  
  <p><strong>The problem</strong>: I loose the focus when adding an item. So if I tab from friendly name to name and a new row is added, the focus is lost from name. How can I solve this?</p>
  
  <p><strong>EDIT</strong>: Underneath some code to show my problem. I want to be able to TAB from cell to cell. When both cells of a row are left empty, I want to remove that line. At the end I want to have an empty row (both cells empty). At this point the code works, but loses focus if you use tab. And working with a listbox makes that TAB doesn't work to go to the next item in list.</p>
  
  <p>XAML:</p>
  
  <pre><code>&lt;Window x:Class="Focus.MainWindow"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      xmlns:local="clr-namespace:Focus"
      xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
           xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
      mc:Ignorable="d" 
      d:DataContext="{d:DesignInstance Type=local:MainViewModel, IsDesignTimeCreatable=True}"
      Title="MainWindow" Height="350" Width="525"&gt;
  &lt;Window.Resources&gt;
      &lt;DataTemplate x:Key="DataTemplate1"&gt;
          &lt;Grid&gt;
              &lt;Grid.ColumnDefinitions&gt;
                  &lt;ColumnDefinition /&gt;
                  &lt;ColumnDefinition Width="5"/&gt;
                  &lt;ColumnDefinition /&gt;
              &lt;/Grid.ColumnDefinitions&gt;
              &lt;TextBox LostKeyboardFocus="TextBox_LostKeyboardFocus"&gt;
                  &lt;TextBox.Text&gt;
                      &lt;Binding Path="FriendlyName" UpdateSourceTrigger="PropertyChanged"/&gt;
                  &lt;/TextBox.Text&gt;
              &lt;/TextBox&gt;
              &lt;TextBox Grid.Column="2" LostKeyboardFocus="TextBox_LostKeyboardFocus"&gt;
                  &lt;TextBox.Text&gt;
                      &lt;Binding Path="Name" UpdateSourceTrigger="PropertyChanged"/&gt;
                  &lt;/TextBox.Text&gt;
              &lt;/TextBox&gt;
          &lt;/Grid&gt;
      &lt;/DataTemplate&gt;
  
  &lt;/Window.Resources&gt;
  &lt;Grid&gt;
      &lt;ListBox Margin="10" ItemsSource="{Binding OrderedItems}" ItemTemplate="{DynamicResource DataTemplate1}" HorizontalContentAlignment="Stretch"&gt;
  
      &lt;/ListBox&gt;
  &lt;/Grid&gt;
  </code></pre>
  
  <p></p>
  
  <p>Code behind:</p>
  
  <pre><code>using System.Windows;
  using System.Windows.Input;
  
  namespace Focus
  {
      public partial class MainWindow : Window
      {
          public MainWindow()
          {
              InitializeComponent();
              this.DataContext = new MainViewModel();
          }
  
          private void TextBox_LostKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e)
          {
              MainViewModel vm = this.DataContext as MainViewModel;
              vm.CheckToAddEmptyItem();
          }        
      }
  }
  </code></pre>
  
  <p>The ViewModel</p>
  
  <pre><code>using System.Collections.Generic;
  using System.ComponentModel;
  using System.Linq;
  
  namespace Focus
  {
      public class MainViewModel : INotifyPropertyChanged
      {
          private List&lt;MyItem&gt; _myItems = new List&lt;MyItem&gt;();
          public IEnumerable&lt;MyItem&gt; OrderedItems
          {
              get { return _myItems.OrderBy(i =&gt; i.IsEmpty); }
          }
  
          internal void CheckToAddEmptyItem()
          {
              int count = _myItems.Count(i =&gt; i.IsEmpty);
  
              if (count == 0)
              {
                  _myItems.Add(new MyItem());
  
                  if (null != PropertyChanged)
                      PropertyChanged(this, new PropertyChangedEventArgs("OrderedItems"));
              }
              else if (count &gt; 1)
              {
                  var items = _myItems.Where(i =&gt; i.IsEmpty).Skip(1).ToArray();
  
                  foreach (MyItem item in items)
                      _myItems.Remove(item);
  
                  if (null != PropertyChanged)
                      PropertyChanged(this, new PropertyChangedEventArgs("OrderedItems"));
              }
          }
  
          public event PropertyChangedEventHandler PropertyChanged;
  
          public MainViewModel()
          {
              for(int i=1; i &lt;= 5; ++i)
              {
                  _myItems.Add(new MyItem() { FriendlyName = "Item #" + i, Name = "ITEM" + i });
              }
  
              if (null != PropertyChanged)
                  PropertyChanged(this, new PropertyChangedEventArgs("OrderedItems"));
  
              CheckToAddEmptyItem();
          }
      }
  }
  </code></pre>
  
  <p>The MyItem class:</p>
  
  <pre><code>using System.ComponentModel;
  
  namespace Focus
  {
      public class MyItem : INotifyPropertyChanged
      {
          public event PropertyChangedEventHandler PropertyChanged;
  
          private string _name = string.Empty;    
          public string Name
          {
              get { return _name; }
              set
              {
                  if (value != _name)
                  {
                      _name = value;
                      if (null != PropertyChanged)
                      {
                          PropertyChanged(this, new PropertyChangedEventArgs("Name"));
                          PropertyChanged(this, new PropertyChangedEventArgs("IsEmpty"));
                      }
                  }
              }
          }
  
          private string _friendlyName = string.Empty;
          public string FriendlyName
          {
              get { return _friendlyName; }
              set
              {
                  if (value != _friendlyName)
                  {
                      _friendlyName = value;
                      if (null != PropertyChanged)
                      {
                          PropertyChanged(this, new PropertyChangedEventArgs("FriendlyName"));
                          PropertyChanged(this, new PropertyChangedEventArgs("IsEmpty"));
                      }
                  }
              }
          }
  
          public bool IsEmpty
          {
              get { return string.IsNullOrEmpty(Name) &amp;&amp; string.IsNullOrEmpty(FriendlyName); }
          }
      }
  }
  </code></pre>