title: ComboBox of Types with a ValueConverter and Custom Attributes
body: <p>I'm using MVVM, in case it makes a difference.</p>
  
  <p>My <em>MainWindowViewModel</em> has two DependencyProperties, <strong>TheList</strong>, and <strong>TheSelectedItem</strong>. <strong>TheList</strong> is a <em>List&lt;Type&gt;</em>, <strong>TheSelectedItem</strong> is a <em>Type</em>.</p>
  
  <p>The MainWindow has a <em>ComboBox</em>. When the <em>MainWindowViewModel</em> loads it grabs a list of all the classes in the assembly that implement <em>IMyInterface</em> and sets <strong>TheList</strong> to this. </p>
  
  <p>Each of these classes has a custom attribute applied called <em>DisplayName</em>, which has one parameter, that will be used to show a user-friendly name for the class instead of the name the application knows about for the class.</p>
  
  <p>I've also got a <em>ValueConverter</em> for the express purpose of converting these types into the display names.</p>
  
  <pre><code>    public class TypeToDisplayName : IValueConverter
      {
          public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
          {
              if (targetType.Name == "IEnumerable")
              {
                  List&lt;string&gt; returnList = new List&lt;string&gt;();
                  if (value is List&lt;Type&gt;)
                  {
                      foreach (Type t in value as List&lt;Type&gt;)
                      {
                          returnList.Add(ReflectionHelper.GetDisplayName(t));
                      }
                  }
  
                  return returnList;
              }
              else
              {
                  throw new NotSupportedException();
              }
          }
  
          public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
          {
              return typeof(BasicTemplate);
          }
      }
  </code></pre>
  
  <p>So, what I wind up with is a <em>ComboBox</em> with a list of names in it that the user should be able to understand. Awesome! This is just what I want! </p>
  
  <p>Next step: I bind the SelectedItem property of my <em>ComboBox</em> to my <strong>TheSelectedItem</strong> property in my ViewModel.</p>
  
  <p>Here's the problem: When I make a selection, I get a little red box around my ComboBox and the <strong>TheSelectedItem</strong> property on my ViewModel never gets set. </p>
  
  <p>I'm pretty sure it's because of a type mismatch (the items in the ComboBox appear to be strings now, and <strong>TheSelectedItem</strong> is of type <em>Type</em>--also, when I change <strong>TheSelectedItem</strong> to a <em>string</em> instead of a <em>Type</em>, it works). But I don't know where I need to start coding to convert the (hopefully unique) DisplayName that's in the ComboBox back to a Type object.</p>
  
  <p>Thanks in advance for any help. I'm pretty stumped on this one.</p>