title: Silverlight Databinding a Combobox to an Enum
body: <p>I have run into a problem where a couple of combos that are bound to enums in my model are not working properly. To start off let me tell you that I have 2 areas of my app where the enum can be manipulated. For clarity/simplicity, lets just say there are identicle views on the left and right and the results/summary in the center. When I set the value in one of the views it sets the value through the ViewModel to the Model, as designed, but the combo in the view on the opposite side (again just for clarity/simplicity's sake) doesn't seem to have updated. It should have selected the appropriate row/item, but doesn't. Below are snippets of code that I'm currently using: </p>
  
  <pre><code>//Definition of Enum:
  public enum eNumericAndDateOperator 
  { GreaterThan, GreaterThanOrEqualTo, Equals, LessThanOrEqualTo, LessThan, Between, Ignore }
  
  
  //XAML of Combo in View(s):
  &lt;ComboBox&gt;
     &lt;ComboBoxItem Content="" IsSelected="{Binding Path=SelectedOperator, Mode=TwoWay, Converter={StaticResource EnumConverter}, ConverterParameter=Ignore}" /&gt;
     &lt;ComboBoxItem Content="&amp;lt;" IsSelected="{Binding Path=SelectedOperator, Mode=TwoWay, Converter={StaticResource EnumConverter}, ConverterParameter=LessThan}" /&gt;
     &lt;ComboBoxItem Content="&amp;lt;=" IsSelected="{Binding Path=SelectedOperator, Mode=TwoWay, Converter={StaticResource EnumConverter}, ConverterParameter=LessThanOrEqualTo}" /&gt;
     &lt;ComboBoxItem Content="=" IsSelected="{Binding Path=SelectedOperator, Mode=TwoWay, Converter={StaticResource EnumConverter}, ConverterParameter=Equals}" /&gt;
     &lt;ComboBoxItem Content="&amp;gt;=" IsSelected="{Binding Path=SelectedOperator, Mode=TwoWay, Converter={StaticResource EnumConverter}, ConverterParameter=GreaterThan}" /&gt;
     &lt;ComboBoxItem Content="&amp;gt;" IsSelected="{Binding Path=SelectedOperator, Mode=TwoWay, Converter={StaticResource EnumConverter}, ConverterParameter=GreaterThanOrEqualTo}" /&gt;
     &lt;ComboBoxItem Content="Between" IsSelected="{Binding Path=SelectedOperator, Mode=TwoWay, Converter={StaticResource EnumConverter}, ConverterParameter=Between}" /&gt;
  &lt;/ComboBox&gt;
  
     //Enum Converter code
     public class EnumToBoolConverter : IValueConverter
     {
        #region Methods
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
           if (value == null || parameter == null)
              return value;
           return value.ToString() == parameter.ToString();
        }
        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
           if (value == null || parameter == null)
              return value;
  
           return Enum.Parse(targetType, parameter.ToString(), true);
        }
        #endregion Methods
     }
  </code></pre>
  
  <p>EnumConverter is your basic Enum to Bool converter class (and yes with ConvertBack defined).</p>
  
  <p>Should I be using the Selected Index on the combo instead? I can't really load the enum as the combo's ItemSource through an EnumToIEnumerableConverter or something because I want to customize the visable text for each item...</p>