title: Change listbox template on lost focus event in WPF
body: <p>I want to present a listbox with TextBlocks as items. When the user clicks/selects an item it changes into a TextBox for editing. As soon as the controls loses focus the item would turn back to a TextBlock.</p>
  
  <p>The following XAML is almost working in that the TextBlock does turn into a TextBox when it's selected. It also turns back to a TextBlock if I select another item on the list. The problem is that if I move out of the listbox (in this case to the Add New text box) the list item stays as a TextBox.</p>
  
  <p>The question ( <a href="https://stackoverflow.com/questions/466555/wpf-listviewitem-lost-focus-event-how-to-get-at-the-event">WPF ListViewItem lost focus event - How to get at the event?</a> ) looked promising but I can't make it work. I tried using the IsFocused property but then I wasn't able to edit the textbox because when I got into the textbox the listboxitem would go out of focus and thus turning back to a TextBlock before I had a chance to edit the text.</p>
  
  <p>How can I make the TextBox turn back to TextBlock if the listbox/item loses the focus?</p>
  
  <p>(Any other implementation that accomplishes the goal are also welcomed)</p>
  
  <pre><code>&lt;Window x:Class="MyView.MainWindow1"
          xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
          xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
          xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
          Title="MainWindow1" Height="300" Width="200"&gt;
      &lt;Window.Resources&gt;
          &lt;DataTemplate x:Key="ItemTemplate"&gt;
              &lt;TextBlock Text="{Binding Name}" /&gt;
          &lt;/DataTemplate&gt;
  
          &lt;DataTemplate x:Key="SelectedTemplate"&gt;
              &lt;TextBox Text="{Binding Name, Mode=TwoWay}" /&gt;
          &lt;/DataTemplate&gt;
  
          &lt;Style TargetType="{x:Type ListBoxItem}" x:Key="ContainerStyle"&gt;
              &lt;Setter Property="ContentTemplate" Value="{StaticResource ItemTemplate}" /&gt;
              &lt;Style.Triggers&gt;
                  &lt;Trigger Property="IsSelected" Value="True"&gt;
                      &lt;Setter Property="ContentTemplate" Value="{StaticResource SelectedTemplate}" /&gt;
                  &lt;/Trigger&gt;
              &lt;/Style.Triggers&gt;
          &lt;/Style&gt;
      &lt;/Window.Resources&gt;
  
      &lt;StackPanel &gt;
          &lt;ListBox ItemsSource="{Binding Departments}" HorizontalContentAlignment="Stretch"
                   ItemContainerStyle="{StaticResource ContainerStyle}" /&gt;
          &lt;Grid&gt;
              &lt;Grid.ColumnDefinitions&gt;
                  &lt;ColumnDefinition Width="*" /&gt;
                  &lt;ColumnDefinition Width="Auto" /&gt;
              &lt;/Grid.ColumnDefinitions&gt;
              &lt;TextBox  Text="{Binding NewDepartmentName, Mode=TwoWay}"  /&gt;
              &lt;Button Grid.Column="1" Width="50" Content="Add" Command="{Binding Path=AddNewDepartmentCommand}" /&gt;
          &lt;/Grid&gt;
      &lt;/StackPanel&gt;
  &lt;/Window&gt;
  </code></pre>