title: Problem with binding Nullable value to WPF ComboBox
body: <p>I am binding a WPF ComboBox to a nullable property of type MyEnum? (where MyEnum is an enumerated type)</p>
  
  <p>I am programmatically populating the ComboBox items like this:</p>
  
  <pre><code>// The enum type being bound to 
  enum MyEnum { Yes, No }
  
  // Helper class for representing combobox listitems
  // (a combination of display string and value)
  class ComboItem {
    public string Display {get;set}
    public MyEnum? Value {get;set}
  }
  
  private void LoadComboBoxItems()
  {
    // Make a list of items to load into the combo
    var items = new List&lt;ComboItem&gt; {
      new ComboItem {Value = null, Display = "Maybe"},
      new ComboItem {Value = MyEnum.Yes, Display = "Yes"},
      new ComboItem {Value = MyEnum.No, Display = "No"},};
  
    // Bind the combo's items to this list.
    theCombo.ItemsSource = items;
    theCombo.DisplayMemberPath = "Display";
    theCombo.SelectedValuePath = "Value";
  }
  </code></pre>
  
  <p>Also in the code-behind, I am setting the DataContext to an instance of a class with a property called TheNullableProperty (for this example anyway) of type MyEnum?.</p>
  
  <p>The binding of theCombo's SelectedValue is done in my XAML file.</p>
  
  <pre><code>&lt;ComboBox 
    Name="theCombo" 
    SelectedValue="{Binding Path=TheNullableProperty,
                            UpdateSourceTrigger=PropertyChanged}"/&gt;
  </code></pre>
  
  <p><strong>Problem:</strong></p>
  
  <p>When the value of the bound property is initially non-null, the combo box displays the value properly.</p>
  
  <p>But when the value of the bound property is initially null, the combo box is blank.</p>
  
  <p>It looks like every aspect of data binding is working apart from the representation of the null value when the combobox is first shown.</p>
  
  <p>For example: you can select Maybe from the dropdown, and the bound property is correctly set to null. It's just that initial loading that's failing. Maybe I need to just manually set the SelectedValue initially...</p>
  
  <h1>What I Ended Up Doing</h1>
  
  <ul>
  <li>Add a hidden textblock databound to the underlying nullable enum value via a Converter that converts from the nullable enum to a string (enum.ToString, or "null").</li>
  <li>Load up the combo box with 'ComboItems' each having a string Label (displayed in the combo) and a string Value equal to the enum values as strings (and "null" for the null value).</li>
  <li><p>Data-bind the combo box to the textblock.</p>
  
  <pre><code>/// &lt;summary&gt;
  /// Convert from EnumeratedType? to string (null-&gt;"null", enum values-&gt;ToString)
  /// &lt;/summary&gt;
  public class EnumConverter&lt;T&gt; : IValueConverter where T:struct 
  {
    public static string To(T? c)
    {
      if (c == null)
        return "null";
      return c.ToString();
    }
  
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
      return To((T?)value);
    }
  
    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
      var s = (string) value;
      if (s == "null")
        return null;
      return (T?)Enum.Parse(typeof(T), s);
    }
  }
  
  public class MyEnumConverter : EnumConverter&lt;MyEnum&gt;
  {
  }
  
  public class ComboItem
  {
    public string Value { get; set; }
    public string Label { get; set; }
  
    public ComboItem(MyEnum? e, string label)
    {
      Value = MyEnumConverter.To(e);
      Label = label;
    }
  }
  
  static IEnumerable&lt;ComboItem&gt; GetItems()
  {
    yield return new ComboItem(null, "maybe");
    yield return new ComboItem(MyEnum.Yes, "yup");
    yield return new ComboItem(MyEnum.No, "nope");
  }
  
  private void SetupComboBox()
  {
    thecombo.ItemsSource = GetItems().ToList();
  }
  </code></pre></li>
  </ul>