title: XAML binding issues with connected comboboxes
body: <p>I have two comboboxes: Categories and Types.  When my form is initially displayed, I am listing all categories and types that exist in the database.  For both comboboxes, I manually insert row 0 to be of value “All” so that they don’t have to choose either if they don’t want to.</p>
  
  <p>I have both comboboxes bound to ReactiveObjects so that if the user selects a Category, the Types combobox is automatically re-populated with a query to show only types relevant to the selected Category along with the row 0 added.</p>
  
  <p>When the user selects a Category, it runs the query properly, returns the relevant Types, adds the row 0 properly and the combobox is populated correctly; however, on the XAML size it’s not selecting the row 0, and it adds the red outline around the combobox signifying an invalid selection was made.</p>
  
  <p>If no choice is made for the Type combobox and the form is submitted, the correct value of 0 is passed.  So while everything is working properly, the red box around the Types combobox is communicating to the user that they did something wrong and I cannot determine why the XAML isn’t picking up the selected values.  I have run the code without it adding the row 0 and it still has the same behavior, i.e., the combobox is populated correctly, but no row is selected and the red outline appears.</p>
  
  <p><strong>XAML for comboboxes</strong></p>
  
  <pre><code>&lt;ComboBox 
      Grid.Row="3" 
      Grid.Column="1" 
      Width="200" 
      HorizontalAlignment="Left" 
      VerticalAlignment="Top"
      Style="{StaticResource SimpleComboBox}"
      ItemsSource="{Binding Categories}"
      SelectedValue="{Binding SearchCriteria.CategoryID}"
      SelectedValuePath="ComboValueID"
      DisplayMemberPath="ComboDataValue"
      /&gt;
  
  &lt;TextBlock 
      Grid.Row="3" 
      Grid.Column="2" 
      Style="{StaticResource NormalTextNarrow}" 
      Text="Type" VerticalAlignment="Top" 
      /&gt;
  
  &lt;ComboBox 
      Grid.Row="3" 
      Grid.Column="3" 
      Width="200" 
      HorizontalAlignment="Left" 
      VerticalAlignment="Top"
      Style="{StaticResource SimpleComboBox}"
      ItemsSource="{Binding Types}"
      SelectedValue="{Binding SearchCriteria.TypeId}"
      SelectedValuePath="ComboValueID"
      DisplayMemberPath="ComboDataValue"
      /&gt;
  </code></pre>
  
  <p><strong>Relevant VM code</strong></p>
  
  <pre><code>// Definition of SearchCriteria.  ResourceItem is a ReactiveObject and 
  // all of the relevant properties watch for changes in values.
  private ResourceItem searchCriteria;
  public ResourceItem SearchCriteria
  {
      get { return searchCriteria; }
      set { this.RaiseAndSetIfChanged(ref searchCriteria, value); }
  }
  
  // This all happens in my constructor
  
  // Defining Row 0
  var b = new GenericCombobox { ComboValueID = 0, ComboDataValue = "All" };
  
  // Populating the comboboxes from the database
  Categories = omr.GetKTValues("RES_CATEGORIES");
  Types = omr.GetKTValuesRU("RES_TYPES");
  
  // Adding the row 0    
  Categories.Insert(0, b);
  Types.Insert(0, b);
  
  // The form is displayed correctly at this point with the row 0 selected
  </code></pre>
  
  <p><strong>Problem Code</strong></p>
  
  <pre><code>// When the user picks a category, this is the method that is invoked:
  private void categoryChanged()
  {
      if (SearchCriteria.CategoryID != 0)
      {
          Types = rr.GetCategoryTypes(SearchCriteria.CategoryID);
          SearchCriteria.TypeId = 0;
      }
  }
  
  // This runs correctly and returns the relevant Types
  public List&lt;GenericCombobox&gt; GetCategoryTypes(int categoryId)
  {
      string sql = "res.usp_GetCategoryTypes";
      var types = new List&lt;GenericCombobox&gt;();
  
      SqlConnection sqlCn = DatabaseCommunication.OpenConnection();
  
      using (SqlCommand cmd = new SqlCommand(sql, sqlCn))
      {
          // Omitting db stuff for brevity...
          try
          {
              SqlDataReader dr = cmd.ExecuteReader();
              while (dr.Read())
              {
                  types.Add(new GenericCombobox
                  {
                      ComboValueID = (int)dr["TypeId"],
                      ComboDataValue = (string)dr["Type"],
                      IsSelected = false,
                      Description = (string)dr["Type"],
                      ComboDataCode = (string)dr["Type"]
                  });
              }
              // More db-stuff omitted
          }
  
          // Adding the row 0
          var b = new GenericCombobox { ComboValueID = 0, ComboDataValue = "All", IsSelected = false, Description = "All", ComboDataCode = "All" };
          types.Insert(0, b);
  
          return types;
      }
  </code></pre>
  
  <p><strong>Update with Additional Code</strong></p>
  
  <pre><code>// Object containing the TypeId property
  public class ResourceItem : ReactiveObject, ISelectable
  {
      public int Id { get; set; }
      public int? OriginalItemId { get; set; }
  
      // ...many other properties...
  
      private int typeId;
      public int TypeId
      {
          get { return typeId; }
          set { this.RaiseAndSetIfChanged(ref typeId, value); }
      }
  
      // ...and many more follow...
  </code></pre>