title: XAML ComboBox SelectedIndex Binding to ViewModel property
body: <p>I have tried for what seems a lifetime to get this issue sorted but unfortunately to no avail.. even after searching stackoverflow and other sites.</p>
  
  <p>Ill try and keep my issue short but to the point.</p>
  
  <p>Using WPF and the MVVM methodology I have : A View and a ViewModel.</p>
  
  <p>From within the view XAML I am binding a comboBox to a list of values in a key/value dictionary object. This works fine - I am able to see the values drop down when I click on the combo box.</p>
  
  <p>Note: Each record can have more than 1 instalment in a gridview where the combobox is displayed.</p>
  
  <pre><code>&lt;dxg:GridColumn Header="Instalments" 
          Width="100" 
          AllowSorting="False" 
          AllowAutoFilter="False"&gt;
  &lt;dxg:GridColumn.CellTemplate&gt;
  &lt;DataTemplate&gt;
      &lt;ComboBox IsEnabled="{Binding Data.IsIncludedInDD}"
                Name="cbInstalmentValues" 
                Width="200"
                ItemsSource="{Binding Data.D_InstalmentValues}"
                SelectedIndex="{Binding Data.Instalments, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" 
                DisplayMemberPath="Value" 
                SelectedValuePath="Key"
                SelectedValue="{Binding SelectedInstalmentValue, Mode=TwoWay, ValidatesOnDataErrors=True, NotifyOnValidationError=True, UpdateSourceTrigger=PropertyChanged}"
                VerticalAlignment="Center" /&gt;
  </code></pre>
  
  <p>Code:</p>
  
  <pre><code>private static readonly Dictionary&lt;int, string&gt; D_instalmentValues = new Dictionary&lt;int, string&gt;
  {
    {0, "None"}, 
    {1, "1 Instalment"}, 
    {2, "2 Instalments"}, 
    {3, "3 Instalments"}, 
    {4, "4 Instalments"},
    {5, "5 Instalments"}, 
    {6, "6 Instalments"}, 
    {7, "7 Instalments"}, 
    {8, "8 Instalments"}, 
    {9, "9 Instalments"},
    {10, "10 Instalments"}, 
    {11, "11 Instalments"}, 
    {12, "12 Instalments"}
  };
  
  public Dictionary&lt;int, string&gt; D_InstalmentValues 
  {
    get
    {
      return _instalmentValues;
    }
    set
    {
      _instalmentValues = value;
    }
  }
  </code></pre>
  
  <p>So..when I load up my application I want the comboBox to read the value from the 'Instalments' property in the viewmodel and bind to the same index of the value in the dictionary and display the number of instalments.. ie: None, 1 Instalment etc..</p>
  
  <pre><code>public int Instalments
  {
      get
      {
          return _chargeTemplate.Instalments;
      }
  
      set
      {
      if (value == -1)
      {
          value = value + 1;
          _chargeTemplate.Instalments = _chargeTemplate.Instalments;
      }
      else
      {
          if (_chargeTemplate.Instalments == value)
          {
              return;
          }
          else
          {
              _chargeTemplate.Instalments = value;
          }
       }
       //NotifyNotRegister("Instalments");
       RaisePropertyChanged("Instalments");
      }
  }
  </code></pre>
  
  <p>Now the '_chargeTemplate.Instalments' value holds the value from the Instalments column from the record in the database.</p>
  
  <p>The combobox does not seem to bind if the value in the Instalments property is 0. The box is blank.</p>
  
  <p>It does get populated after I select a record which has a value greater than 0.</p>
  
  <p>When I tried populating all the instalment fields in the sql table with values from 1 to 12 the selected index did not get set properly for the very first record displayed in my application but if I selected the next record it was displayed ok... then if I reselected the first record again it was also displayed correctly.</p>
  
  <p>Another strange part of the behaviour was for the first record, if I had 3 instalments, and in the sql table instalment 1 = 2, instalment 2 = 4 and instalment 3 = 0... the first 2 instalments for that record had their combo box index set but the last record was empty.</p>
  
  <p>In summary - I need to know how, when the application loads - how to make the selected index bind from a viewmodel to the view without the combo box being blank for the first record or for values where the selected index is 0.</p>
  
  <p>I hope there is enough information here... </p>
  
  <p>Many Thanks</p>
  
  <p>UPDATE :</p>
  
  <p>I made a small change to the XAML and the property to which I am binding the combobox selectedindex. This allows the 0 value to display 'None' in the combobox where the value is 0 for a persons instalment value in the sql database.</p>
  
  <p>However the very first person displayed in the list of records does not have the selectd index set to the index of the dictionary value unless I select another person and reselect the first person again... strange</p>
  
  <p>ViewModel</p>
  
  <blockquote>
    <p>public int Instalments
            {
                get
                {
                    return _chargeTemplate.Instalments;
                }
              }</p>
  </blockquote>
  
  <p>XAML</p>
  
  <blockquote>
    <p>SelectedIndex="{Binding Data.Instalments, Mode=OneWay}" </p>
  </blockquote>