title: Bind a List<string> to a TextBox
body: <p>After 20+ years of windows programming and two days of WPF I feel I know nothing :-)</p>
  
  <p>My first WPF program is really simple: You drop a few files from explorer and their names are shown in a TextBox control.
  (It works fine for a ListBox, but that isn't what I want. And of course adding the lines manually in the Drop event works as well - but I want to learn about the Binding ways..)</p>
  
  <p>So I wrote a Converter but somehow it insn't used (breakpoints won't get hit) and nothing shows up.</p>
  
  <p>It should be a small thing or maybe I'm totally off track. Found many examples for similar things from which i patched this together but still can't get it to work.</p>
  
  <p>(I probably won't need the ConvertBack, but wrote it down anyway..)</p>
  
  <p>Here is the converter class: </p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Text;
  using System.Threading.Tasks;
  using System.Windows.Data;
  
  namespace WpTest02
  {
    public class ListToTextConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
          {
              StringBuilder sb = new StringBuilder();
              foreach (string s in (List&lt;string&gt;)value) sb.AppendLine(s);
              return sb.ToString();
          }
  
        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
          {
              string[] lines = ((string)value).Split(new string[] { @"\r\n" }, StringSplitOptions.RemoveEmptyEntries);
              return lines.ToList&lt;String&gt;();
          }
     }
  
  }
  </code></pre>
  
  <p>The MainWindow.xaml, where I suspect the binding problem to be:</p>
  
  <pre><code>&lt;Window x:Class="WpTest02.MainWindow"
          xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
          xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
          xmlns:local="clr-namespace:WpTest02"
          Title="MainWindow" Height="350" Width="525"
          &gt;
      &lt;Window.Resources&gt;
          &lt;local:ListToTextConverter x:Key="converter1" /&gt;
      &lt;/Window.Resources&gt;
  
      &lt;Grid &gt;
          &lt;TextBox Name="tb_files" Margin="50,20,0,0"  AllowDrop="True" 
                   PreviewDragOver="tb_files_PreviewDragOver" Drop="tb_files_Drop" 
                   Text="{Binding Path=fileNames, Converter={StaticResource converter1} }"
                   /&gt;
      &lt;/Grid&gt;
  &lt;/Window&gt;
  </code></pre>
  
  <p>And the Codebehind with nothing more the the data property to bind to and the drag&amp;drop code, which works.</p>
  
  <pre><code>using System; 
  //etc ..
  
  namespace WpTest02
  {
      public partial class MainWindow : Window
      {
          public MainWindow()
          {
              InitializeComponent();
              fileNames = new List&lt;string&gt;();
          }
  
  
          public List&lt;string&gt; fileNames { get; set; }
  
          private void tb_files_Drop(object sender, DragEventArgs e)
          {
              var files = ((DataObject)e.Data).GetFileDropList();
              foreach (string s in files) fileNames.Add(s);
  
              // EDIT: this doesn't help ? Wrong!                     
              // EDIT: this is actually necessary! :                     
              tb_files.GetBindingExpression(TextBox.TextProperty).UpdateTarget();
  
              // this obviosly would work:
              //foreach (string s in files) tb_files.Text += s + "\r\n";
          }
  
          private void tb_files_PreviewDragOver(object sender, DragEventArgs e)
          {
              e.Handled = true;
          }
  
      }
  }
  </code></pre>
  
  <p><strong>Note</strong>: I have editied the last piece of code to stress the importance of the <code>UpdateTarget</code> call.</p>