title: Binding ComboBox Text property using a converter
body: <p>I'm having troubles using a WPF ComboBox in the following scenario:</p>
  
  <p><strong>ViewModel</strong></p>
  
  <ul>
  <li><p>Provides a <code>ObservableCollection&lt;T&gt;</code>; This collection contains a list of items the user may select.</p></li>
  <li><p>Provides a property of type <code>T</code> representing the selected item.</p></li>
  </ul>
  
  <p>The user shall be able to select either an existing item from the items in the <code>ObservableCollection&lt;T&gt;</code> or add a new item by typing in the string representation.</p>
  
  <p>I have a converter available able to convert a item of type <code>T</code> to <code>string</code> and vice versa.</p>
  
  <p><strong>View</strong></p>
  
  <p>My ComboBox bound to the collection and the selected item properties:</p>
  
  <pre><code>&lt;ComboBox ItemsSource="{Binding Path=MyObservableCollection}"
            SelectedItem="{Binding Path=MySelectedItem}"
            IsReadOnly="False" IsEditable="True"/&gt;
  </code></pre>
  
  <p>The data template used to display the items correctly:</p>
  
  <pre><code>&lt;DataTemplate DataType="{x:Type T}"&gt;
      &lt;TextBlock Text="{Binding Converter={StaticResource ResourceKey=MyConverter}}"/&gt;
  &lt;/DataTemplate&gt;
  </code></pre>
  
  <p><strong>The Problem</strong></p>
  
  <p>Items in the drop down list of the <code>ComboBox</code> are displayed correctly using the convert. The selected item displayed in the <code>TextBox</code> of the <code>ComboBox</code> is not displayed correctly; Instead of using my converter, the <code>ToString</code> method is used.</p>
  
  <p>Is it possible to specify a converter for the Text property? I tried using the following code: </p>
  
  <pre><code>&lt;ComboBox ItemsSource="{Binding Path=MyObservableCollection}"
            SelectedItem="{Binding Path=MySelectedItem}"
            Text="{Binding Path=MySelectedItem, Converter={StaticResource ResourceKey=MyConverter}}"
            IsReadOnly="False" IsEditable="True"/&gt;
  </code></pre>
  
  <p>This solves the display problem but now I get the <code>Type.FullName</code> of T in the converters <code>ConvertBack</code> method - which can of course not be converted.</p>
  
  <p><strong>Summary</strong></p>
  
  <p>I want the user to be able to select an item from a collection, allowing him to add new items by entering the string representation. The items in the collection shall be converted between string and object representation using a converter. The conversion shall be done in both the drop down list and the text box of the ComboBox.</p>
  
  <p><strong>Edit</strong></p>
  
  <p>Here is the code in my converter - no magic there just straightforward conversion:</p>
  
  <pre><code>public class MyConverter : IValueConverter
  {
  
      public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
      {
          return MyConverter.Convert(value as T);
      }
  
      public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
      {
          return MyConverter.Convert(value as string);
      }
  
      public static string Convert(T key)
      {
          // Conversion from T to string.
      }
  
      public static T Convert(string key)
      {
          // Conversion from string to T.
      }
  
  }
  </code></pre>
  
  <p>Ok, now I found something that does what I want:</p>
  
  <pre><code>        &lt;TextBox Text="{Binding Path=MySelectedItem, Converter={StaticResource ResourceKey=MyConverter}}"/&gt;
          &lt;ListBox ItemsSource="{Binding Path=MyObservableCollection}"
                   SelectedItem="{Binding Path=MySelectedItem, Converter={StaticResource ResourceKey=MyConverter}}"/&gt;
  </code></pre>
  
  <p>This does exactly what I want; I can select predefined values and the user may add values on his own. Is it possible to do this with a ComboBox?</p>