title: WPF: How to populate combobox with enum in Xaml
body: <p>I know there are several ways to do it, but I would like to make it even easier if possible because I have a lot of comboboxes to bind in this way. There is a suggestion using ObjectDataProvider <a href="http://www.wpftutorial.net/BindEnumValues.html" rel="nofollow">here</a>. The problem is that I have to create a resource entry for each enum and that's a lot. So far, I have been using the code-behind way because it's much shorter:</p>
  
  <pre><code>cmb.ItemsSource = Enum.GetValues(typeof(MyTypes));
  </code></pre>
  
  <p>I'm wondering if an equivalent can be produced in Xaml. I thought we could archive this by using a converter. We could convert the type to an array and then bind the array to combobox' ItemsSource. But I got stuck on how to specify my enum to the converter. Here is my code:</p>
  
  <p>My enum:</p>
  
  <pre><code>public enum MyTypes { Type1, Type2, Type3 }; 
  </code></pre>
  
  <p>This is my converter:</p>
  
  <pre><code>public class EnumToArrayConverter : IValueConverter
  {
      public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
      {
          return Enum.GetValues(value.GetType());
      }
  
      public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
      {
          return null; // I don't care about this
      }
  }
  </code></pre>
  
  <p>My Xaml Resource:</p>
  
  <pre><code>&lt;lib:EnumToArrayConverter x:Key="E2A"/&gt;
  </code></pre>
  
  <p>Here is how to use it:</p>
  
  <pre><code>&lt;ComboBox SelectedItem="{Binding MyType}" ItemsSource="{Binding MyTypes, Converter={StaticResource E2A}}"/&gt;
  </code></pre>
  
  <p>So, my question is how to specify my enum "MyTypes" to the converter. I also tried to prepend namespace, but it doesn't help.</p>