title: Two-way binding combobox to enum
body: <p>I have a View that is linked to my ViewModel using a DataTemplate, like this</p>
  
  <pre><code>&lt;DataTemplate DataType="{x:Type ViewModels:ViewModel}"&gt;
      &lt;Views:View /&gt;
  &lt;/DataTemplate&gt;
  </code></pre>
  
  <p>The ViewModel holds a property <code>ProcessOption</code> that is of type <code>MyEnum?</code>, where <code>MyEnum</code> is a custom enumeration that has let's say 3 values: <code>Single</code>, <code>Multiple</code> and <code>All</code>. I am trying to bind a combobox to this property, so the approach I am following is:</p>
  
  <p>ViewModel has a property of <code>List&lt;string&gt;</code> that is </p>
  
  <pre><code>public List&lt;string&gt; Options
      {
          get 
          {
              return Enum.GetNames(typeof(MyEnum)).ToList();
          }
      }
  </code></pre>
  
  <p>to which I bind the <code>ItemsSource</code> property of the <code>Combobox</code>. Then, in addition to the <code>ProcessOption</code> property, the ViewModel also has an <code>OptionName</code> property (of <code>string</code>), which is intended to hold the selected option's name. The ViewModel implements <code>INotifyPropertyChanged</code> and both properties raise the <code>PropertyChanged</code> event in their setters. The binding I am using then is:</p>
  
  <pre><code>&lt;ComboBox ItemsSource="{Binding Options}"
      SelectedItem="{Binding OptionName}"
      SelectedValue="{Binding ProcessOption}"/&gt;
  </code></pre>
  
  <p>This works fine up to this point. Initially the combobox is empty and both properties are <code>null</code>, and when the user selects an option this is propagated to the ViewModel as it should.</p>
  
  <p>The problem appears when I load the data from a Database, and I want to load the controls with initial values. In this case, in the ViewModel's constructor I have this:</p>
  
  <pre><code>this.ProcessOption = objectFromDB.ProcessOption // this is the value restored from DB,        let's say it is MyEnum.Multiple
  this.OptionName = Options.First(x =&gt; x.Equals(Enum.GetName(typeof(MyEnum), objectFromDB.ProcessOption)));
  </code></pre>
  
  <p>The problem is, although the above sets the two properties to their correct values, they are later set to null from the <code>Combobox</code> binding, so the initial values are not kept. I have also tried to do something like <code>if (value == null) { return; }</code> in the properties' setters, in which case they have the correct values after the View loads, however the <code>Combobox</code> still does not display the correct option, it is empty.</p>
  
  <p>I should also note that I've also tried setting <code>IsSynchronisedWithCurrentItem</code> and it doesn't make any difference, apart from the fact that the first element is displayed instead of the combobox being empty.</p>
  
  <p>Can anyone help with this binding? Any help will be very much appreciated, this is driving me crazy!</p>